Showing Forms


Any form ”that is, any class that derives from the Form base class ”can be shown in one of two ways. Here, it is shown modelessly:

 void button1_Click(object sender, System.EventArgs e) {   AnotherForm form = new AnotherForm();  form.Show(); // Show form modelessly  } 

Here, a form is shown modally:

 void button1_Click(object sender, System.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: Dialogs).

[1] However, if the main form is closed, Application.Run will close all other forms and return.

Owner and Owned Forms

As the ShowDialog method shows the new form, it uses the currently active form as the new form's logical owner. [2] An owner is a window that contributes to the behavior of the owned form. For example, if an owner has a modal child, then activating the owner, such as by using the Alt+Tab task-switching keystroke, activates the owned form. In the modeless case, when the owner form is minimized or restored, so is the owned form. Also, an owned form is always shown on top of an owner form, even if the owner is currently active, as if the user has clicked on the owner, as shown in Figure 2.1.

[2] The form shown using ShowDialog will have an Owner property set to null if no owner is provided explicitly. However, the user interaction behavior for modal forms is the same whether or not the Owner property is set.

Figure 2.1. Owner-Owned Relationship

When a form is activated modelessly via the Show method, by default the new form does not have an owner. Setting the owner of a modeless form is a matter of setting the new form's Owner property:

 void button1_Click(object sender, System.EventArgs e) {   AnotherForm form = new AnotherForm();  form.Owner = this; // Establish owner/owned relationship  form.Show(); } 

In the modal case, in spite of the implicit owner-owned relationship that WinForms creates, the modal form will have a null Owner property unless the Owner property is set explicitly. You can do this by setting the Owner property just before the call to ShowDialog or by passing the owner form as an argument to the ShowDialog override that takes an IWin32Window [3] parameter:

[3] IWin32Window is an interface exposed by UI objects in WinForms that expose a Win32 HWND via the IWin32Window.Handle property.

 void button1_Click(object sender, System.EventArgs e) {   AnotherForm form = new AnotherForm();  form.ShowDialog(this); // Passing the owner as an argument  } 

An owner form can enumerate the list of forms it owns using the OwnedForms collection:

 void button1_Click(object sender, System.EventArgs e) {   AnotherForm form = new AnotherForm();   form.Owner = this;   form.Show();  foreach( Form ownedForm in this.OwnedForms ) {  MessageBox.Show(ownedForm.Text);  }  } 

You may have noticed that in addition to an optional owner, a form can have an optional parent, as exposed via the Parent property. As it turns out, normal forms have a Parent property that is always null. The one exception to this rule is MDI child forms, which I discuss later. Unlike the owner-owned relationship, the parent-child relationship dictates clipping ”that is, a child's edge is clipped to the edge of the parent, as shown in Figure 2.2.

Figure 2.2. A Child ListBox Control Clipped to the Client Area of Its Parent Form

The parent-child relationship is reserved for parent forms (or parent container controls) and child controls (with the exception of MDI, which is discussed later).



Windows Forms Programming in C#
Windows Forms Programming in C#
ISBN: 0321116208
EAN: 2147483647
Year: 2003
Pages: 136
Authors: Chris Sells

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net