How can I retrieve a value from a modal dialog?
First, add buttons to a dialog and specify a value in the buttons' DialogResult property.
Then check the return value from the dialog using the DialogResult enumeration.
For example, in C#:
MyForm form = new MyForm();
if (form.ShowDialog() == DialogResult.OK)
{
// Code
}And in Visual Basic .NET:
Dim form As New MyForm()
If form.ShowDialog = DialogResult.OK Then
' Code
End If
Note: If your application is localized, you should use the MM .NET Form Manager's Show and ShowDialog methods rather than calling these methods directly on the form. For details, see the MM .NET topic Showing Localized Windows Forms
If the user clicks the Close button [X], the modal dialog is hidden and its DialogResult property is set to DialogResult.Cancel. When a user closes the dialog this way, or when the Close button of a dialog is clicked, the form's Close() method is not automatically called. Instead the form is hidden, allowing you to retrieve values from public form-level properties. To release the form, you can call its Dispose method.
For more information on getting results from modal dialogs, see the .NET Help topic "Closing Dialog Boxes and Retaining User Input".
© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 10/17/06
Comment or report problem with topic
