Bagikan melalui


User Input to Dialog Boxes

Dialog boxes are typically used to prompt the user for data that is then used by the application. The form displaying the dialog box processes that information. When you display a dialog box, it is important to know how it is closed, or its "result." For example, if the user clicks a Cancel button, the data entered by the user is disposed of rather than retained. You can use the Form.DialogResult property to find out how a dialog box is closed.

From the form that displays the dialog box (known as the dialog box's parent form), you can use the value of the DialogResult property to determine if an OK button or a Cancel button is clicked. Based on the DialogResult returned, you decide whether you need to retrieve the dialog box's information. You can set the value of a dialog box's DialogResult property when you do not use Button controls on your dialog box but still want to return a DialogResult.

When a form is displayed as a modal dialog box, clicking the Close button (the button with an X in the top-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. The Close method is not automatically called when the user clicks the Close button of a dialog box or sets the value of the DialogResult property. Instead, the form is hidden and can be shown again without creating a new instance of the dialog box. Because of this behavior, you must call the Dispose method of the form when the form is no longer needed by your application.

Note

You can override the value assigned to the DialogResult property when the user clicks the Close button by setting the DialogResult property in an event handler for the Closing event of the form.

See Also

Tasks

How to: Create Dialog Boxes at Design Time

How to: Close Dialog Boxes and Retain User Input

How to: Retrieve the Result for Dialog Boxes

Other Resources

Dialog Boxes in Windows Forms