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.
You can set the dialog 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 manually set the result of the dialog box at run time.
To set the DialogResult property for a control or form at run time
[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;
}
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-handling method’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
[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(System.Object sender, System.EventArgs e) { this.DialogResult = DialogResult.None; }
Dialog Boxes | How to Handle User Input to Dialog Boxes | Creating Dialog Boxes | Retrieving the Result for Dialog Boxes | Retrieving Dialog Box Information Selectively Using Multiple Properties | Retrieving Dialog Box Information Collectively Using Objects | Retrieving Information from the Parent Form of a Dialog Box