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:

 
 Sub button1_Click(sender As Object, e As EventArgs)   Dim myform As AnotherForm = New AnotherForm()   myform.Show() ' Show form modelessly End Sub 

Here, a form is shown modally:

 
 Sub button1_Click(sender As Object, e As System.EventArgs)   Dim myform As AnotherForm = New AnotherForm()  myform.ShowDialog()  '  Show form modally  End Sub 

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:

 
 Sub button1_Click(sender As Object, e As System.EventArgs)   Dim myform As AnotherForm = New AnotherForm()   myform.Owner = Me  ' Establish owner/owned relationship   myform.Show() End Sub 

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.

 
 Sub button1_Click(sender As Object, e As System.EventArgs)   Dim myform As AnotherForm = New AnotherForm()   myform.ShowDialog(Me)  ' Passing the owner as an argument End Sub 

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

 
 Sub button1_Click(sender As Object, e As System.EventArgs)   Dim myform As AnotherForm = New AnotherForm()   myform.Owner = Me   myform.Show()   Dim ownedForm as Form  For Each ownedForm in Me.OwnedForms  MessageBox.Show(ownedForm.Text)  Next  End Sub 

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 don't ever have a parent ”in other words, the Parent property is always Nothing. The one exception to this rule is MDI child forms, which we discuss later. Unlike the owner-owned relationship, the parent-child relationship dictates clipping ”that is, a child's edge will be 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 Visual Basic .NET
Windows Forms Programming in Visual Basic .NET
ISBN: 0321125193
EAN: 2147483647
Year: 2003
Pages: 139

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