On C # non-modal form the difference between show () and Mode form ShowDialog () _c# Tutorial

Source: Internet
Author: User

The dialog box is not modal or modeless. modal dialog box, which must be closed (hidden or unloaded) before it can continue to operate on other parts of the application. For example, if a dialog box requires that you click OK or cancel before you can switch to another form or dialog box, it is modal.

One, how to call

Any form (the class that derives the form from the base class) can be displayed in two ways.
Non-modal form
From Qform=new Form ();
Qform. Show ();
Mode form
Form Qform=new form ();
Qform. ShowDialog ();

First, the difference in control rights

Form.show After creating a new form (not mode), returns immediately with no relationship between the currently active form and the new form, which is OK to close (or minimize) an existing form or to close (or minimize) a new window while retaining the new window.

Form.ShowDialog creates a modal form, that is, the original form can regain control only when a new window is established. That is, if you do not close a new window, you will not be able to perform any action on the original active window. The new window is minimized, and the restore will be done with the original window, but the closing of the new window has no effect on the original window.

Note that no matter what the situation is, as long as the main form is closed or the main program is finished, Application.Run will close all forms, whether it is modal or modeless.

Second, what does the owner attribute bring

The above is the form that does not establish the owning relationship. When you create an ownership relationship between forms, things change.

1, first look at the situation of the non-mode. the way to establish an owning relationship for a modeless new window is to modify its owner property. (The non-modal window does not exist by default)

Form. Owner=this; Assuming the current window is the owner of the new window
Form. Show ();

Obviously, the new modeless form is already considered a subform of the original active form, and the behavior of the original window will affect the new window, so let's call it the relationship between the parent window and the child window.

So what is the obvious change after the change? There are two main points:

First, the parent window is minimized, restored, or closed, and the child window is minimized, restored, or closed. (Note that they are irrelevant until you add an owning relationship.) Conversely, the minimized, restored, or closed child windows do not affect the parent window.

Second, on the taskbar, only the icon for the parent form is displayed without displaying the subform's icon. (Before the birth of a parent-child relationship, each form has its own icon on the taskbar.) )

2, the mode form of the case. when you display a new form with the ShowDialog method, the current form is considered to be the logical owner of the new form. The "logical owner" means that by default, the owner value is null when explicitly specifying the owner with ShowDialog. However, the user's interaction behavior is the same regardless of whether the Owner property is set or not.

The method for setting the Owner property is passed as a ShowDialog parameter in addition to the one above the show scenario. Such as:

Form. ShowDialog (this); The current form is the owner of the new form.

That is, if you specify that the third form is owner of the new mode window, it is true that the new window and the original window may be severed, but as a child window of the third window. But in fact, the establishment of the parent-child relationship does not bring us much surprises. For example, before the new window closes, the parent window is still unable to gain control, and so on, all behavior has not changed.

Iii. Summary and explanation

1, the Show method created by the behavior of the form is uncertain, owner property is responsible for this important.

2. There is an innate relationship between the current active window and the modal window created with ShowDialog, and the bearer of this relationship can change, but the establishment or release of the relationship does not bring any change to the behavior of the form.

3. A form can have an optional owner and can be the owner of multiple forms.

4. The subform and the parent form referred to here are not in the true sense, but in the unscientific notation coined to deepen the understanding. Should be separated from the parent form and subform area in the window form terminology, and should not be confused. The latter has edge cropping.

The above article discusses the C # modeless form the show () and the Mode form ShowDialog () The difference is the small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.