DialogResult
When showing a dialog form, you'll often need to get information about what action the user selected. Windows Forms has a built-in property for that purpose. When a form is shown with the ShowDialog method, the form has a property called DialogResult to indicate its state.
The DialogResult property can take the following enumerated results:
|
> |
DialogResult. |
.Abort |
|
> |
DialogResult. |
.Cancel |
|
> |
DialogResult. |
.Ignore |
|
> |
DialogResult. |
.No |
|
> |
DialogResult. |
.None |
|
> |
DialogResult. |
.OK |
|
> |
DialogResult. |
Retry |
|
> |
DialogResult. |
.Yes |
When the DialogResult property is set, the dialog is hidden as a result. That is, setting the DialogResult property causes an implicit call to the Hide method of the dialog form, so that control is released back to the form that called the dialog.
- FiGURE 14-3
The DialogResult property of a dialog box can be set in two ways. The most common way is to associate a DialogResult value with a button. Then, when the button is pressed, the associated value is automatically placed in the DialogResult property of the form.
To set the DialogResult value associated with a button, use the DialogResult property of the button. If this property is set for the button, then it is unnecessary to set the DialogResult in code when the button is pressed.
The following example uses this technique. In Visual Studio 2010, start a new VB Windows application. On the automatic blank form that appears (named Form1), place a single button and set its Text property to Dialog.
Now add a new Windows form by selecting Project O Add Windows Form and name it DialogForm.vb. Place two buttons on DialogForm and set the properties for the buttons as shown in the following table.
|
property |
value for first button |
value for second button |
Name OKButton CancelButton
Text OK Cancel
Name OKButton CancelButton
Text OK Cancel
DialogResult OK Cancel
Do not put any code in DialogForm at all. The form should look like the one shown in Figure 14-4.
On the first form, Form1, place the following code in the Click event for Button1:
Dim frmDialogForm As New DialogForm() frmDialogForm.ShowDialog()
' You're back from the dialog - check user action. Select Case frmDialogForm.DialogResult Case DialogResult.OK
MsgBox("The user pressed OK") Case DialogResult.Cancel
MsgBox("The user pressed cancel") End Select frmDialogForm = Nothing
Run and test the code. When a button is pressed on the dialog form, a message box should be displayed (by the calling form) indicating the button that was pressed.
The second way to set the DialogResult property of the form is in code. In a Button_Click event, or anywhere else in the dialog form, a line like the following can be used to set the DialogResult property for the form and simultaneously hide the dialog form, returning control to the calling form:
Me.DialogResult = DialogResult.Ignore
This particular line sets the dialog result to DialogResult.Ignore, but setting the dialog result to any of the permitted values also hides the dialog form.
Post a comment