The difference between C # Show () and ShowDialog ()

Source: Internet
Author: User
A.winform in the form display
There are 2 ways to display the form:
Form.ShowDialog method (The form is displayed as a modal form)
Form.show method (The form is displayed as a modeless form)

The two specific differences are as follows:
1. After calling the Form.show method, the code following the Show method executes immediately
2. After calling the Form.ShowDialog method, the code following this method is not executed until the dialog box is closed

Small bet

1, after the modal display,
A pop-up window blocks all message responses from the calling window.
The window must be called before the popup window can continue.
After the modal window is "closed," you can read the information in the modal window, including the window's return status, and the value of the window's child control.


An 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) hides the form and sets the DialogResult property to DialogResult.Cancel. Unlike a modeless form, the. NET Framework does not call the Close method when the user clicks the Close Form button on the dialog box or sets the value of the DialogResult property. The form can be hidden and re-displayed instead of creating a new instance of the dialog box. Because a form that appears as a dialog box is not closed, you must call the Dispose method of the form when it is no longer needed by your application.

That is, when the non-modal window is closed, the Close method is called, and the Dispose method is called, and the window resource is reclaimed, so the window information cannot be obtained after the window is closed. When the modal window is closed, the Close method is not called, the Dispose method is not called, the window still exists, and the resource is occupied, so you can continue to get information about the window, which needs to be released manually when the window is no longer in use


2, after the non-modal display,
You can switch between the pop-up window and the calling window at will.
The following code can be executed immediately after the call window calls the Show method.
After the non-modal window is closed, all of the window's resources are freed and the window does not exist to get any information about the window.

3, so when the form to show to use a singleton mode, each time show finished, close () Form, at the time of the second show will appear: unable to access the disposed object. Object name: "XXXX". At this point, you should modify close () to form hide.

The above is the difference between C # Show () and ShowDialog (), more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.