On the difference between C non modal form show of and modal form showdialog of

  • 2021-10-27 08:38:01
  • OfStack

Dialog boxes are either modal or modeless. The modal dialog box must be closed (hidden or uninstalled) before you can continue with other parts of the application. For example, if a dialog box requires you to click OK or Cancel before you can switch to another form or dialog box, it is modal.

1. How to call

Any form (a class derived from the base class Form) can be displayed in two ways.
//Non-modal forms
From qform=new Form();
qform.Show();
//Modal form
Form qform=new Form();
qform.ShowDialog();

1. Differences in control

Form. Show Returns immediately after a new form is created (non-mode) without establishing any relationship between the currently active form and the new form, i.e. closing (or minimizing) an existing form while keeping the new window or closing (or minimizing) the new window while keeping the existing form.

Form. ShowDialog creates a modal form, that is, the original form can regain control only after the created new window is closed. That is, if you don't close the new window, you can't do anything about the original active window. Minimize and restore the new window from the original window, but the closing of the new window has no effect on the original window.

Note that Application. Run will close all forms, whether modal or non-modal, in any case, as long as the main form is closed or the main program ends.

2. What does the Owner attribute bring

What I said above is a form without establishing a ownership relationship. When a ownership relationship is established between forms, the situation will change.

1. First, look at the non-mode situation. The way to establish an ownership relationship for a new non-modal window is to modify its Owner property. (By default, no owner exists for non-modal windows)

form. Owner=this; //Assume that the current window is the owner of the new window
form.Show();

Obviously, the new non-modal form is already considered a child of the original active form, and the behavior of the original window will affect the new window, so let's call them the relationship between the parent window and the child window.

So, what significant changes will occur after the changes? There are two main points:

First, the parent window is minimized, restored or closed, and the child window will be minimized, restored or closed accordingly. (Note that they are not related to each other until the ownership relationship is added.) Conversely, minimizing, restoring, or closing a child window has no effect on the parent window.

2. On the taskbar, only the icons of the parent form are displayed, not the icons of the child forms. (Before the birth of the parent-child relationship, each form had its own icon on the taskbar.)

2. In the case of modal forms. When a new form is displayed using the ShowDialog method, the current form is considered the logical owner of the new form. The so-called logical owner means that by default, if the owner is explicitly specified with ShowDialog, the value of Owner is null. However, whether the Owner property is set or not, the interaction behavior with users is the same.

There is another way to set the Owner property besides the same as in the case of Show mentioned above, that is, to pass it 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 third form as the Owner of the new mode window, it is true that the new window may be disconnected from the original window and appear as a child window of the third window. But in fact, the establishment of this father-son relationship between them has not brought us much surprise in behavior. For example, before the new window is closed, the parent window still cannot gain control, etc., and the 1-cut behavior has not changed.

3. Summary and explanation

1. The form created by the Show method has behavioral uncertainty, for which the Owner property is responsible.

2. There is an innate relationship between the current active window and the mode window created with ShowDialog. The bearer of this relationship can be changed, but the establishment or dissolution of this relationship cannot bring any change to the behavior of the form.

3. A form can have one optional owner and can be the owner of multiple forms at the same time.

4. The child form and parent form referred to here are not in the true sense, but unscientific names invented for deepening understanding. It should be distinguished from the parent form and child form in window form terminology and should not be confused. The latter has edge clipping.


Related articles: