Relationships Between Forms Windows Forms I: Developing Desktop Applications

145 Fired when the form is loaded. Its syntax is: Protected Overrides Sub OnLoadByVal e As System.EventArgs MaximizedBoundsChanged Fired when the value of the forms MaximizedBounds property which determines the size of the maximized form is changed. Its syntax is: Protected Overrides Sub OnMaximizedBoundsChanged _ ByVal e As System.EventArgs MaximumSizeChanged Fired when the value of the forms MaximumSize property which defines the maximum size to which the form can be resized is changed. Its syntax is: Protected Overrides Sub OnMaximumSizeChangedByVal e As System.EventArgs MdiChildActivate Fired when an MDI child window is activated. Its syntax is: Protected Overrides Sub OnMdiChildActivateByVal e As System.EventArgs MenuComplete Fired when menu selection is finished. Its syntax is: Protected Overrides Sub OnMenuCompleteByVal e As System.EventArgs MenuStart Fired when a menu is displayed. Its syntax is: Protected Overrides Sub OnMenuStartByVal e As System.EventArgs MinimumSizeChanged Fired when the value of the forms MinimumSize property which defines the minimum size to which the form can be resized is changed. Its syntax is: Protected Overrides Sub OnMinimumSizeChangedByVal e As System.EventArgs

4.3 Relationships Between Forms

The Form class has two properties that control a forms 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 parents edges, just like any other control. The code in Ex am ple 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 Figur e 4- 6 . 146 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.RunNew MyParentForm End Sub End Module Public Class MyParentForm Inherits Form Public Sub New Set my size. Me.ClientSize = New System.Drawing.Size600, 400 Create and show a child form. Dim frm As New MyChildFormMe frm.Show End Sub End Class Public Class MyChildForm Inherits Form Public Sub NewByVal 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 parents 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 forms FormBorderStyle property. 147 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 FindReplace- type dialog boxes. The code in Ex am ple 4- 5 creates an ownerowned 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.RunNew MyOwnerForm End Sub End Module Public Class MyOwnerForm Inherits Form Public Sub New Set my size. Me.ClientSize = New System.Drawing.Size600, 450 Create and show an owned form. Dim frm As New MyOwnedFormMe frm.Show End Sub End Class Public Class MyOwnedForm Inherits Form Public Sub NewByVal Owner As Form Set my owner. Me.Owner = Owner End Sub End Class

4.4 MDI Applications