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 Form1() {   // Required for Windows Form Designer support   InitializeComponent();  // TODO: Add any constructor code after InitializeComponent call  // Adding a control   Button anotherButton = new Button();   this.Controls.Add(anotherButton);   // Changing a property   this.Text = "Something Not Known At Design Time"; } 

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:

 void InitializeComponent() {   ...  this.Load += new System.EventHandler(this.Form1_Load);  ... }  void Form1_Load(object sender, System.EventArgs e) {  MessageBox.Show("Welcome to Form1!"); } 

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.

 void Form1_Load(object sender, EventArgs e) {  // Don't show this form   this.Visible = false;   this.ShowInTaskbar = false;  } 

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.

 void InitializeComponent() {   ...  this.Activated += new System.EventHandler(this.Form1_Activated);  ... }  void Form1_Activated(object sender, System.EventArgs e) {  this.game.Resume();  }  

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

 void InitializeComponent() {   ...  this.Deactivate += new System.EventHandler(this.Form1_Deactivate);  ... }  void Form1_Deactivate(object sender, System.EventArgs e) {  this.game.Pause();  }  

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:

 void hideButton_Click(object sender, System.EventArgs e) {  this.Hide(); // Set Visible property indirectly   this.Visible = false; // Set Visible property directly  } 

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:

 void Form1_Closing(object sender, CancelEventArgs e) {   DialogResult res = MessageBox.Show(     "Abort your game?", "Game In Progress", MessageBoxButtons.YesNo);   e.Cancel = (res == DialogResult.No); } void Form1_Closed(object sender, EventArgs e) {   MessageBox.Show("Your game was aborted"); } 

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 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