Form Lifetime


Although the user can't see a form until either Show or ShowDialog is called, a form exists as soon as the object is created. A new form object wakes up in the object's constructor , which the runtime calls when an object is first created. It's during the constructor that InitializeComponent is called and therefore when all the child controls are created and initialized .

It's a bad idea to put custom code into the InitializeComponent function because the Designer is likely to throw it away. However, if you'd like to add other controls or change anything set by the InitializeComponent method, you can do that in the constructor. If the initial form implementation was generated by one of the VS.NET wizards, you'll even have a helpful comment indicating where the Designer thinks that you should add your initialization code:

 
 Public Sub New  ()  MyBase  .  New()  'This call is required by the Windows Form Designer.  InitializeComponent()  ' Add any initialization after the InitializeComponent() call   ' Adding a control   Dim anotherButton As Button = New Button()   Me.Controls.Add(anotherButton)   ' Changing a property   Me.Text = "Something Not Known At Design Time" End Sub 

When Form.Show or Form.ShowDialog is called, that's the form's cue to show itself as well as all its child controls. You can be notified that this has happened when the code handles the Load event:

 
 Sub Form1_Load(sender As System.Object, e As System.EventArgs)_ Handles MyBase.Load   MessageBox.Show("Welcome to Form1!") End Sub 

The Load event is useful for doing any final initialization right before a form is shown. Also, the Load event is a good place to change the Visible property and the ShowInTaskbar property if you'd like the form to start as hidden: [4]

[4] Starting a form as hidden is useful for forms that need to be running but that shouldn't show themselves right away. An example is a form with a notify icon in the taskbar.

 
 Sub Form1_Load(sender As Object, e As EventArgs)   ' Don't show this form   Me.Visible = False   Me.ShowInTaskbar = False End Sub 

When a form is shown, it will become the active form. It's the active form that receives keyboard input. An inactive form is made active when users click on it or otherwise indicate to Windows that they would like it to be active, such as by using Alt+Tab to switch to it. You can make an inactive form active programmatically by using the Form.Activate method. [5] When a form is made active, including when the form is first loaded, it receives the Activated event:

[5] Older implementations of Win32 allowed an application to set itself active on top of the currently active window, something that could be pretty annoying. Modern implementations of Win32 allow an application to set a window as active only if another window in that application is currently active. Some of these implementations flash a background application's button on the shell's taskbar to indicate that the application would like your attention.

 
 Sub Form1_Activated(sender As Object, e As System.EventArgs)_       Handles MyBase.Activated   Me.game.Resume() End Sub 

If an application has a form that is the current active window as far as the operating system is concerned , you can discover that using the Form.ActiveForm shared method. If Form.ActiveForm is Nothing, it means that none of your application's forms is currently active. To track when a form deactivates, handle the Deactivate event:

 
 Sub Form1_Deactivate(sender As Object, e As System.EventArgs) _       Handles MyBase.Deactivate   Me.game.Pause() End Sub 

If, in addition to controlling whether or not a form is active, you'd like to control its visibility, either you can use the Hide and Show methods , which set the Visible property, or you can set the Visible property directly:

 
 Sub hideButton_Click(sender As Object, e As System.EventArgs) _  Me.Hide()  ' Set Visible property indirectly   Me.Visible = False  ' Set Visible property directly  End Sub 

As you might expect, there is an event that you can handle as your form flickers in and out of visual reality. It's called VisibleChanged. The Activated, Deactivate, and VisibleChanged events are all handy for restarting and pausing activities that require user interaction or attention, such as in a game. To stop an activity altogether, you'll want to handle the Closing or the Closed event. The Closing event can be canceled if users change their minds:

 
 Sub Form1_Closing(sender As Object, e As CancelEventArgs) _       Handles MyBase.Closing   Dim res As DialogResult = MessageBox.Show( _       "Abort your game?", "Game in Progress", _       MessageBoxButtons.YesNo)   e.Cancel = (res = DialogResult.No) End Sub Sub Form1_Closed(sender As Object, e As System.EventArgs) _       Handles MyBase.Closed   MessageBox.Show("Your game was aborted") End Sub 

Notice that during the Closing event the handler can set the CancelEventArgs.Cancel property to true, canceling the closing of the form. This is also the best place to serialize a form's visible properties, such as size and location, before Windows closes the form. On the other hand, the Closed event is merely a notification that the form has already gone away.



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