Relationships Between Forms

The Form class has two properties that control a form’s relationship to other forms: the Parent property (inherited from the Control class) and the Owner property. Setting the Parent property causes the constrained form to appear only within the bounds of the parent—and always to appear on top of the parent. This gives an effect similar to MDI applications (which have other features as well and are discussed later in this chapter). When a form has a parent, it can be docked to the parent’s edges, just like any other control. The code in Example 4-4 demonstrates this. It can be compiled from the command line with this command:

vbc filename.vb /r:System.dll,System.Drawing.dll,System.Windows.Forms.dll /t:winexe

The result is displayed in Figure 4-6.

A form with a parent

Figure 4-6. A form with a parent

Example 4-4. Creating a form with a parent

Imports System.Drawing
Imports System.Windows.Forms

Module modMain
   <System.STAThreadAttribute(  )> Public Sub Main(  )
      System.Threading.Thread.CurrentThread.ApartmentState = _
         System.Threading.ApartmentState.STA
      System.Windows.Forms.Application.Run(New MyParentForm(  ))
   End Sub
End Module

Public Class MyParentForm
   Inherits Form
   Public Sub New(  )
      ' Set my size.
      Me.ClientSize = New System.Drawing.Size(600, 400)
      ' Create and show a child form.
      Dim frm As New MyChildForm(Me)
      frm.Show(  )
   End Sub
End Class

Public Class MyChildForm
   Inherits Form
   Public Sub New(ByVal Parent As Control)
      ' TopLevel must be False for me to have a parent.
      Me.TopLevel = False
      ' Set my parent.
      Me.Parent = Parent
      ' Dock to my parent's left edge.
      Me.Dock = DockStyle.Left
   End Sub
End Class

If the child form is maximized, it expands to fill the parent form. If the child form is minimized, it shrinks to a small rectangle at the bottom of the parent window. Because the child form in this example has a title bar and a sizable border, it can be moved and sized even though it has been docked. This behavior can be changed by modifying the form’s FormBorderStyle property.

Setting the Owner property of a form causes another form to own the first. An owned form is not constrained to appear within the bounds of its owner, but when it does overlay its owner, it is always on top. Furthermore, the owned form is always minimized, restored, or destroyed when its owner is minimized, restored, or destroyed. Owned forms are good for floating-tool windows or Find/Replace-type dialog boxes. The code in Example 4-5 creates an owner/owned relationship. Compile it with this command:

vbc filename.vb /r:System.dll,System.Drawing.dll,System.Windows.Forms.dll /t:winexe

Example 4-5. Creating a form with an owner

Imports System.Drawing
Imports System.Windows.Forms

Module modMain
   <System.STAThreadAttribute(  )> Public Sub Main(  )
      System.Threading.Thread.CurrentThread.ApartmentState = _
         System.Threading.ApartmentState.STA
      System.Windows.Forms.Application.Run(New MyOwnerForm(  ))
   End Sub
End Module

Public Class MyOwnerForm
   Inherits Form
   Public Sub New(  )
      ' Set my size.
      Me.ClientSize = New System.Drawing.Size(600, 450)
      ' Create and show an owned form.
      Dim frm As New MyOwnedForm(Me)
      frm.Show(  )
   End Sub
End Class

Public Class MyOwnedForm
   Inherits Form
   Public Sub New(ByVal Owner As Form)
      ' Set my owner.
      Me.Owner = Owner
   End Sub
End Class

Get Programming Visual Basic .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.