C # non-modal form the difference between show () and modal Form ShowDialog () (GO)

Source: Internet
Author: User

The dialog box is not modal or modeless. modal dialog box, which must be closed (hidden or uninstalled) until you can continue to manipulate other parts of the application. For example, if you have a dialog box that requires you to click OK or cancel before you can switch to another form or dialog box, it is modal.
First, how to call
Any form (a class that derives from the base class form) can be displayed in two ways.
Non-modal forms
From Qform=new Form ();
Qform. Show ();
Mode form
Form Qform=new form ();
Qform. ShowDialog ();
The difference in control right
Form.show returns immediately after a new form is created (non-modal), and does not have any relationship between the current active form and the new form, that is, it is possible to close (or minimize) an existing form with a new window, or to close (or minimize) a new window in the case of retaining an existing form.
Form.ShowDialog creates a modal form in which the original form can regain control only after a new window has been established. That is, if you do not close the new window, you will not be able to take any action on the original active window. Minimizing and restoring a new window will be done with the original window, but the closing of the new window has no effect on the original window.
It is important to note that, in either case, 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 non-modal.
Ii. What the owner attribute brings
The above is a form that does not establish an owning relationship. When you establish an ownership relationship between forms, the situation changes.
1, first look at the non-modal situation. The way to establish an owning relationship for a new non-modal window is to modify its owner property. (By default, the non-modal window does not exist by the owner)
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 a relationship between the parent window and the child window.
So, what is the significant 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 the owning relationship is added.) Conversely, the minimized, restored, or closed child window does not affect the parent window.
Second, on the taskbar, only the icon for the parent form is displayed, not the child form. (Before a parent-child relationship is born, each form has its own icon on the taskbar.) )
2. In the case of the mode form. When a new form is displayed with the ShowDialog method, the current form is considered to be the logical owner of the new form. The so-called logic owner, is the default, with ShowDialog and explicitly specify the owner, the owner value is null. But regardless of whether the Owner property is set or not, the interaction with the user behaves the same.
The method for setting the Owner property is, in addition to the one shown in the above show case, passed as a parameter of ShowDialog. Such as:
Form. ShowDialog (this); The current form is the owner of the new form.
That is, if you specify the owner of the third form as the new modal window, it is true that the new window and the original window may be severed from the relationship, but instead appear as a child window of the third window. But in fact, the establishment of the parent-child relationship between them did not bring us much surprise in the act. 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 behavior of the form created by the Show method is nondeterministic, and the owner attribute is of great responsibility.
2. There is a innate relationship between the current active window and the modal window created with ShowDialog, the bearer of this relationship can be changed, but the establishment or dissolution of this 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, here refers to the subform and the parent form is not the real sense, but to deepen the understanding and invented the unscientific method of calling. Should be separate from the parent form and subform area in the window form term, and cannot be confused. The latter has edge clipping.

C # non-modal form the difference between show () and modal Form ShowDialog () (GO)

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.