Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

Chapter 14 Managing Windows Forms and Controls at Run Time 359


user—Cancel, Yes, No, Abort, and so on. Each dialog box button can be associated with a
different type of action in the main program. And in each of the dialog box’s button event
procedures, you can assign the DialogResult property for the form that corresponds to the
button name, such as the following program statement:

Me.DialogResult = DialogResult.Cancel 'user clicked Cancel button

In the calling event procedure—in other words, in the Button3_Click event procedure of
LuckySeven .vb—you can write additional program code to detect which button the user
clicked in the dialog box. This information is stored in the form’s DialogResult property,
which can be evaluated using a basic decision structure such as If... Then or Select... Case.
For example, the following code can be used in the Button3_Click event procedure to verify
whether the user clicked OK, Cancel, or another button in the dialog box. (The first line isn’t
new, but reminds you of the HelpInfo form name that you are using in this example .)

My.Forms.HelpInfo.ShowDialog()

If HelpInfo.DialogResult = DialogResult.OK Then
MsgBox("The user clicked OK")
ElseIf HelpInfo.DialogResult = DialogResult.Cancel Then
MsgBox("The user clicked Cancel")
Else
MsgBox("Another button was clicked")
End If

By using creative event procedures that declare, open, and process dialog box choices, you
can add any number of forms to your programs, and you can create a user interface that
looks professional and feels flexible and user friendly.

Positioning Forms on the Windows Desktop


You’ve learned how to add forms to your Visual Basic project and how to open and close
forms by using program code. But which tool or setting determines the placement of
forms on the Windows desktop when your program runs? As you might have noticed, the
placement of forms on the screen at run time is different from the placement of forms within
the Visual Studio development environment at design time. In this section, you’ll learn how
to position your forms just where you want them at run time so that users see just what you
want them to see.

The tool you use isn’t a graphical layout window but a property named DesktopBounds that
is maintained for each form in your project. DesktopBounds can be read or set only at run
Free download pdf