Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Closing Dialog Boxes and Retaining User Input

Closing Dialog Boxes and Retaining User Input

Visual Studio .NET 2003

The way a dialog box is closed, or its "result," can be set at either design time or run time: At design time, you can set the DialogResult property for all of the Button controls on a dialog box. Setting the DialogResult property at run time allows you to dynamically handle user responses.

To set the DialogResult property for a control at design time

  1. Click on the Button control that you want to set the property for.
  2. Select the DialogResult property in the Properties window and open the list of available property settings.
  3. Select the appropriate dialog box result.

You can set the dialog box result for user-performed actions other than clicking a Button control. If your dialog box does not contain buttons to close the dialog box, you can set the result of the dialog box at run time.

To set the DialogResult property for a control or form programmatically

  1. Navigate to the event handler or method that you want to set the DialogResult property for.
  2. Type code similar to the following:
    ' Visual Basic
    Public Sub InformationProcessed()
       ' This code will set the DialogResult for a form.
       Me.DialogResult = DialogResult.Yes
       ' OR
       ' This code will set the DialogResult for a button.
       Button1.DialogResult = DialogResult.No
    End Sub
    
    // C#
    private void InformationProcessed() 
    {
       // This code will set the DialogResult for a form.
       DialogResult = DialogResult.Yes;
       // OR
       // This code will set the DialogResult for a button.
       button1.DialogResult = DialogResult.No;
    }
    
    // C++
    private:
       void InformationProcessed()
       {
          // This code will set the DialogResult for a form.
          this->DialogResult = DialogResult::Yes;
          // OR
          // This code will set the DialogResult for a button.
          button1->DialogResult = DialogResult::No;
       }
    

Although setting the DialogResult property will cause your dialog box to close automatically, you can still handle the control's Click event, and the dialog box will close once the event handler's code is finished. While handling the Click event, you may want to halt the closure of the dialog box.

To stop the DialogResult property from closing the dialog box

  • In the event handler, type code similar to the following:
    ' Visual Basic
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
       Me.DialogResult = DialogResult.None
    End Sub
    
    // C#
    private void button1_Click(object sender, System.EventArgs e) 
    {
       DialogResult = DialogResult.None;
    }
    
    // C++
    private:
       System::Void button1_Click(System::Object *  sender,
          System::EventArgs *  e) 
       {
          this->DialogResult = DialogResult::None;
       }
    
    Note   You can also use the Closing event of the form to stop the closing of a dialog box.

See Also

Dialog Boxes in Windows Forms | User Input to Dialog Boxes | Creating Dialog Boxes | Retrieving the Result for Dialog Boxes | Creating Windows Forms

Show:
© 2015 Microsoft