Any formthat is, any class that derives from the Form base classcan be shown in one of two ways. Here, a form is shown modelessly: void button_Click(object sender, EventArgs e) { AnotherForm form = new AnotherForm(); form.Show(); // Show form modelessly } Here, a form is shown modally: void button_Click(object sender, EventArgs e) { AnotherForm form = new AnotherForm(); form.ShowDialog(); // Show form modally } Form.Show shows the new form modelessly and returns immediately without creating any relationship between the currently active form and the new form. This means that the existing form can be closed, leaving the new form behind.[1] Form.ShowDialog, on the other hand, shows the form modally and does not return control until the created form has been closed, either by using the explicit Close method or by setting the DialogResult property (more on this in Chapter 3).
Owner and Owned FormsAs ShowDialog shows the new form, an implicit relationship is established between the currently active form, known as the owner form, and the new form, known as the owned form. This relationship ensures that the owned form is the active form and is always shown on top of the owner form, as illustrated in Figure 2.1. Figure 2.1. Implicit Owner-Owned Relationship Established by ShowDialog Method
One feature of this relationship is that the owned form affects the behavior of its owner form:
Unlike the ShowDialog method, however, a call to the Show method does not establish an implicit owner-owned relationship. This means that either form can be the currently active form, as shown in Figure 2.2. Figure 2.2. No Owner-Owned Relationship Established by Show Method
Without an implicit owner-owned relationship, owner and owned forms alike can be minimized, maximized, or moved. If the user closes any form other than the main form, the most recently active form is reactivated. Although ShowDialog establishes an implicit owner-owned relationship, there is no built-in way for the owned form to call back to or query the form that opened it. In the modeless case, you can set the new form's Owner property to establish the owner-owned relationship: void button_Click(object sender, EventArgs e) { OwnedForm form = new OwnedForm(); form.Owner = this; // Establish owner-owned relationship form.Show(); }
As a shortcut, you could pass the owner form as an argument to an overload of the Show method, which also takes an IWin32Window parameter: [2]
void button_Click(object sender, EventArgs e) { OwnedForm form = new OwnedForm(); form.Show(this); // Establish owner-owned relationship } The modal case is similar in that you can either set the Owner property explicitly or pass the owner form as an argument to the ShowDialog override: void button_Click(object sender, EventArgs e) { OwnedForm form = new OwnedForm(); // Establish owner-owned relationship // form.Owner = this; form.ShowDialog(this); } An owner form can also enumerate the list of forms it owns using the OwnedForms collection: void button_Click(object sender, EventArgs e) { OwnedForm form = new OwnedForm(); form.Owner = this; form.Show(); foreach( Form ownedForm in this.OwnedForms ) { MessageBox.Show(ownedForm.Text); } } The behavior of forms in an explicit modal owner-owned form relationship is the same as its implicit modal counterpart, but the modeless owner-owned relationship provides additional behavior in the non-owner-owned modeless case. First, the modeless owned form always appears on top of the owner form, even though either can be active. This is useful when you need to keep a form, such as a floating tool window, on top of other forms within an application. [3] Second, if the user presses Alt+Tab to switch from the owner, the owned forms follow suit. To ensure that the user knows which form is the main form, minimizing the owner hides the task bar buttons for all owned forms, leaving only the owner's task bar button visible.
You may have noticed that in addition to an optional owner, a form can have an optional parent, as exposed via the Parent property (which is almost always set to null). This property is reserved for Multiple Document Interface (MDI) forms, discussed later in this chapter, and controls. For controls, the parent-child relationship dictates clippingthat is, a child's edge is clipped to the edge of the parent, as shown in Figure 2.3. Figure 2.3. A Child ListBox Control Clipped to the Client Area of Its Parent Form
|