In VB.NET, a dialog is essentially a modal form. "Modal" means that the dialog form must be closed before other windows in the application can be used again. Dialogs in VB.NET use object oriented properties and methods to tell the calling program what happened in the dialog. The old VB6 architecture just returned a value after it was called to tell your program whether it was successful or not.
The VB6 Way
ReturnValue = showModalDialog()
The VB.NET OOP Way
DialogWindow.ShowDialog()
Debug.WriteLine(AboutBox1.DialogResult)
Most of the time, you'll use a pre-configured dialog such as:
- OpenFileDialog
- SaveFileDialog
- ColorDialog
- FontDialog
- PrintDialog
But you'll find that there are also a lot of cases where you might want to use your own system information in the dialog.
Using the ColorDialog as an example, you use this by adding the component to your project from the Toolbox, configuring the properties as with any other component, and then displaying the dialog and retrieving information from it like this:
If (ColorDialog1.ShowDialog() = DialogResult.OK) Then
textBox1.ForeColor = ColorDialog1.Color
End If
Using a dialog component like this is pretty easy. Just use the properties and methods available in it. Creating those properties and methods is a little harder and that's what this article is about.
The first step in creating your own custom dialog is to add it to your project from the Solution Explorer Add ... menu.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
The only value returned from a dialog by default is the DialogResult property. This is a property available in all forms, but it's not useful unless the window is displayed using the ShowDialog method that displays a modal window. Otherwise, the value is just None. DialogResult is an interesting property because it is part of both Form and Button objects. You can set the design time property of a Button to determine the what's returned when it's clicked. If the Button is contained in a Form that has been displayed using ShowDialog, then the Button DialogResult property is assigned to the Form automatically. When you add a Dialog to your project, two pre-configured buttons are included that work that way. Your design time choices for DialogResult are:
- None
- OK
- Cancel
- Abort
- Retry
- Ignore
- Yes
- No
You can also set DialogResult from any other code in the Dialog class. For example, if you added a LinkLabel component, you could set it with:
Private Sub LinkLabel1_LinkClicked(ByVal ...
Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Sub
If DialogResult is set to any value except None, the dialog form will close and control will return to the parent form at the statement following the ShowDialog method that opened the form. In other words, even the code ...
Me.DialogResult = DialogResult.Retry
... will close the dialog form.
If you want to return other information to the parent form, however, it has to be programmed in the dialog. In general, this information is returned in the standard OOP way, by coding methods and properties. But there's a twist.
Let's start first with a method because that's more straightforward than a property. If you're coding this application, you should have a main form and a dialog form added to your project now. I named the dialog, AccountInfo. Open the code window for the AccountInfo dialog. The OK_Button_Click and Cancel_Button_Click subroutines have already been added. They simply set the value of DialogResult and close the dialog form.
If you want to add a method, just code a public subroutine. This one simply displays the date:
Public Sub displayTheDate()
MsgBox("The Date is: " & _
Now.ToString("MMM dd, yyyy"))
End Sub
It's called by the main routine exactly the way methods always are:
AccountInfo.displayTheDate()
Adding a property to the dialog form, such as the ability to check the state of a Checkbox, involves more problems. We discuss what they are on the next page.

