
Beginning Visual Basic 2005 Express Edition - From Novice To Professional (2006)
.pdf
250 C H A P T E R 9 ■ W I N D O W S A N D D I A L O G S
All that remains is to check the result value, and if the user pressed the No button, meaning “No, I don’t want to close the form down,” you can set e.Cancel to True to cancel the closing:
Private Sub Form1_FormClosing(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
Dim result As DialogResult
result = MessageBox.Show("Are you sure you want to close the form down?", _
"Really quit?", MessageBoxButtons.YesNo, _ MessageBoxIcon.Question)
If result = Windows.Forms.DialogResult.No Then e.Cancel = True
End If
End Sub
That’s all there is to it. Run the application now and try to close the form and you’ll see the message box exactly as you specified it (see Figure 9-17). If you click Yes, the form closes; if you click No, it doesn’t.
Figure 9-17. Message boxes like this are typically used to get important information quickly from the user.
Feel free to play with the various Messagebox.Show() options, and we’ll catch up in the next section.

C H A P T E R 9 ■ W I N D O W S A N D D I A L O G S |
251 |
Creating Your Own Dialog Box
Message boxes are undeniably handy, unless you need to display a custom dialog of exactly your own design. Think back to the Microsoft Word Options dialog from the previous chapter for a minute (see Figure 9-18).
Figure 9-18. The Word Options dialog
There’s no way you are going to be able to achieve all that with a standard message box. You can, however, turn a standard form into a dialog by using almost no code.
First of all, every form has a ShowDialog() method that shows the form modally (remember, a modal form is one that must be closed before any other in the application can be interacted with). In addition, all forms have a DialogResult property that you can set with a DialogResult value. ShowDialog() will return this to you so you can see what the user did with the dialog. In addition though, buttons also have a DialogResult property, which you can set at design time. If the button is clicked at runtime, the form is hidden (not deleted), and its DialogResult value is set into the form’s DialogResult value.

252 C H A P T E R 9 ■ W I N D O W S A N D D I A L O G S
Best of all, don’t forget that when you display a form you are effectively working with an object, an object that can expose properties like any other object. What this means is that you can query those properties after the call to ShowDialog() to get key values from the form. This is all best shown with a “Try it Out.”
Try It Out: Implementing Your Own Custom Dialog
Once again, create a new WinForms project, and then add a second form to the project called Dialog.vb.
Drop a button onto Form1 (the main form) and set its name to getNameButton (see Figure 9-19).
Figure 9-19. Drop a button onto the form that you’ll use to display the dialog at runtime.
Now go back to the designer for the new dialog form. Drop some buttons, a text box, and a label onto the form. Set the captions as in Figure 9-20 and name the text box nameBox (you don’t need to worry about setting up the Name properties for the buttons because you won’t need any code in them).
Figure 9-20. Design your dialog like this. Don’t worry about giving the OK and Cancel buttons
decent names, because they won’t need any code.

C H A P T E R 9 ■ W I N D O W S A N D D I A L O G S |
253 |
Next, press F7 to drop into the code for the form, and add a single property to the form, like this:
Public Class Dialog
ReadOnly Property UserName() As String
Get
Return nameBox.Text
End Get
End Property
End Class
It’s a simple enough property that just grabs the value out of the text box at runtime and returns it.
Go back into the designer view for the dialog now and you’ll set up the buttons to work as dialog buttons are supposed to.
First, click on the form and find the AcceptButton property. This property tells the form which button acts as an OK button and is automatically triggered when the user presses the Enter key on the keyboard. Set it to the OK button. Now find the CancelButton property. This tells the form which button to press when the user presses Esc on the keyboard, just as the user would to quickly get rid of a dialog. Set the CancelButton property to the Cancel button on the form.
Click the OK button now and take a look at its properties. You’ll find a property called DialogResult. If you click on the drop-down in the property entry area, you’ll see all the dialog result values, as in Figure 9-21.
Figure 9-21. You can set the DialogResult property of a button to any of theDialogResult-
enumerated values.
254 C H A P T E R 9 ■ W I N D O W S A N D D I A L O G S
Set it to OK. Next, select the Cancel button and set its DialogResult property to Cancel in the same way.
That’s all there is to it; your dialog is now complete with the only code you had to write being a short bit to expose a property. Time now to code up the main form to run the dialog and do something with it.
Go back to the main form’s designer and double-click the button you dropped on it earlier to go into the code editor for its Click event.
All you want to do here is display your dialog and then have it do something only if the user clicks OK in it. That’s easy to do:
Public Class Form1
Private Sub getNameButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles getNameButton.Click
Dim nameDialog As New Dialog()
If nameDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
End If
End Sub
End Class
As always, the first thing you need to do is create an instance of the form. With that done, you can call ShowDialog() to see whether the result is DialogResult.OK. Let’s add one more line of code now to grab the text the user entered into the dialog from the property that you exposed:
Private Sub getNameButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles getNameButton.Click
Dim nameDialog As New Dialog()
If nameDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
MessageBox.Show("This .NET stuff could really catch on!")
End If
End Sub
Run the program now. When the main form appears, click the button. The dialog form will appear, modally. Try clicking back on the main form while the dialog is on display to confirm that you can’t.

C H A P T E R 9 ■ W I N D O W S A N D D I A L O G S |
255 |
Enter some text now into the text box on the dialog and hit the Enter key, or click the OK button when you are finished. Notice how even though you didn’t write any code to close or hide the dialog, it automatically goes away when you do this. That’s because the OK button has a DialogResult value, and .NET automatically knows that it needs to respond to this button being clicked by closing the form.
The result of the code is that you are returned to the main form and message box, telling you the text that you entered into the dialog (see Figure 9-22).
Figure 9-22. This is what you should see if everything went right.
Common Dialogs
You may have noticed that nearly all Windows applications use the same dialogs. For example, when you go to open a file, you see a file Open dialog that always look the same. Similarly, if you are using a program that lets you choose colors or fonts, the dialogs to let you make the choices are identical from program to program. Before I learned how to develop Windows applications, I naively assumed that developers had to go to great lengths to make their dialogs always look and behave the same way. Instead, those dialogs are part of Windows itself and are available to you within VB Express as handy controls. You can find them all in the Dialogs section of the Toolbox (see Figure 9-23).

256 C H A P T E R 9 ■ W I N D O W S A N D D I A L O G S
Figure 9-23. The common Windows dialogs are all grouped together in the Dialogs section of the Toolbox.
As you might expect by now, they all work exactly the same way as any other dialog (custom or message box). You call ShowDialog() to open up the dialogs at runtime, and then check the result to see whether the user clicked the OK or Cancel buttons. The only differences between the common dialogs from a code point of view are the properties that they expose. The OpenFileDialog and SaveFileDialog controls, for example, expose a property called FileName that is the name of the file the user chose. Both those controls also expose a Filter property that you can use to limit the files that the user sees in the dialogs. For example, if I wanted my user to be able to see .txt files and everything else, I’d set the filter to Text Files|*.txt|All Files|*.*. This means display text files to the user, but filter what the user sees to files ending in .txt. The file dialog displays a File Type drop-down that will also show All Files, which will show all files in the dialog.
The Color dialog, on the other hand, exposes a Color property that can be used to set any other color property you may want. Similarly, the Font dialog contains a Font property.
Let’s take a look with the last “Try It Out” of this chapter.

C H A P T E R 9 ■ W I N D O W S A N D D I A L O G S |
257 |
Try It Out: Working with the Common Dialogs
Start up a new Windows application and drop a label and three buttons on the form, as in Figure 9-24.
Figure 9-24. Arrange your controls like this.
Next, drop OpenFileDialog, FontDialog, and ColorDialog controls onto the form. Notice that instead of appearing on the form, they appear underneath it (see Figure 9-25). These aren’t actually visible controls; you need to call code on them to show the dialogs at runtime.
Figure 9-25. When you drop a common dialog control onto a form, it appears in the area underneath the form.
258 C H A P T E R 9 ■ W I N D O W S A N D D I A L O G S
Double-click the File button you put on the form, and add some code to show the dialog and display the name of the file the user selected:
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
MessageBox.Show("You chose " + OpenFileDialog1.FileName)
End If
End Sub
Straightforward enough, isn’t it. You call ShowDialog() on the control, and so long as the result of the call is that the OK button was pressed, you display the name of the file selected in a message box.
Code up the Font button now (go back to the designer and then double-click the Font button):
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
If FontDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Label1.Font = FontDialog1.Font
End If
End Sub
The code is almost identical, except this time if the OK button is pressed, you take the font that the user selected and set the label’s font to the same thing.

C H A P T E R 9 ■ W I N D O W S A N D D I A L O G S |
259 |
Finally, code up the click handler for the Color button:
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
If ColorDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Me.BackColor = ColorDialog1.Color
End If
End Sub
That’s it. Run the program now to see the dialogs in action (see Figure 9-26).
Figure 9-26. The common Color dialog in action—look familiar?