How to: Retrieve the Result for Dialog Boxes

Once a dialog box is closed, the form that displayed the dialog box can retrieve the result of that dialog box by referencing its DialogResult property or the return value of a call to the ShowDialog method. The form that displayed the dialog box then responds according to the value returned.

To retrieve the DialogResult value

  • Add code similar to the following to the method that displayed the dialog box.

    Typically, this code is placed after the code that creates and displays the dialog box:

    Public Sub DisplayDialog()
       ' Create and display an instance of the dialog box.
       Dim dlg as New Form()
    
       ' Show the dialog and determine the state of the 
       ' DialogResult property for the form.
       If dlg.ShowDialog = DialogResult.OK Then
          ' Do something here to handle data from dialog box.
       End If
    End Sub
    
    private void DisplayDialog() 
    {
       // Create and display an instance of the dialog box
       Form dlg = new Form();
    
       // Show the dialog and determine the state of the 
       // DialogResult property for the form.
       if (dlg.ShowDialog() == DialogResult.OK ) 
       {
          // Do something here to handle data from dialog box.
       }
    }
    
    private void DisplayDialog() 
    {
       // Create and display an instance of the dialog box
       Form dlg = new Form();
    
       // Show the dialog and determine the state of the 
       // DialogResult property for the form.
       if (dlg.ShowDialog() == DialogResult.OK ) 
       {
          // Do something here to handle data from dialog box.
       }
    }
    
    private:
       void DisplayDialog()
       {
          // Create and display an instance of the dialog box
          Form^ dlg = gcnew Form();
    
          // Show the dialog and determine the state of the 
          // DialogResult property for the form.
          if (dlg->ShowDialog() == DialogResult::OK )
          {
             // Do something here to handle data from dialog box.
          }
       }
    

    Note

    Be aware of the importance of calling the Dispose method on the form to properly dispose of the dialog box. Recall that this is not done automatically by clicking the Close box or calling the Close method.

See Also

Tasks

How to: Create Dialog Boxes at Design Time

How to: Close Dialog Boxes and Retain User Input

Concepts

User Input to Dialog Boxes

Other Resources

Dialog Boxes in Windows Forms

Creating a New Windows Form