Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Retrieving the Result for Dialog Boxes

Retrieving the Result for Dialog Boxes

Visual Studio .NET 2003

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 Form.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:

    ' Visual Basic
    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
    
    // C#
    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.
       }
    }
    
    // C++
    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.
          }
       }
    
    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

Dialog Boxes in Windows Forms | User Input to Dialog Boxes | Creating Dialog Boxes | Closing Dialog Boxes and Retaining User Input | Creating Windows Forms

Show:
© 2015 Microsoft