Home > Article > Backend Development > The difference between C# Show() and ShowDialog()
A.Form display in WinForm
There are two methods for displaying a form:
Form.ShowDialog method (the form is displayed as a modal form)
Form.Show method (the form is displayed is a modeless form)
The specific differences between the two are as follows:
1. After calling the Form.Show method, the code following the Show method will be executed immediately
2. After calling the Form.ShowDialog method Finally, the code behind this method is not executed until the dialog box is closed
Small note:
1. After the modal display,
the pop-up window prevents all message responses from the calling window.
The calling window can only continue after the pop-up window ends.
After the modal window is "closed", you can read the information in the modal window, including the return status of the window and the values of the window's sub-controls.
Explanation of showdialog on MSDN:
When the form is displayed as a modal dialog box, clicking the "Close" button (the button with an X in the upper right corner of the form) will hide it form and set the DialogResult property to DialogResult.Cancel. Unlike modeless forms, the .NET Framework does not call the Close method when the user clicks the dialog box's Close Form button or sets the value of the DialogResult property. The form can then be hidden and redisplayed without creating a new instance of the dialog box. Because a form that is displayed as a dialog box is not closed, you must call the form's Dispose method when your application no longer needs the form.
That is to say, when the non-modal window is closed, the close method will be called, and then the dispose method will be called to recycle the window resources. Therefore, after the window is closed, the window information cannot be obtained. . When the modal window is closed, neither the close method nor the dispose method will be called. The window still exists and occupies resources, so you can continue to obtain window-related information. When the window is no longer used, you need to manually release it
2. After non-modal display,
you can switch between the pop-up window and the calling window at will.
After calling the show method in the calling window, the following code can be executed immediately.
After the non-modal window is closed, all resources of the window are released, the window does not exist, and no information about the window can be obtained.
3. Therefore, when the form to be shown uses the singleton mode, after each show is completed, after closing () the form, the second show will appear: Unable Access a released object. Object name: "XXXX". At this time, close() should be modified to hide the form.
The above is the difference between C# Show() and ShowDialog(). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!