7.5 Handling Form Events

 <  Day Day Up  >  

You want to create event handlers for different Windows Forms events.


Technique

Select the form in the Visual Studio .NET IDE form editor and click the lightning bolt toolbar button in the property browser, which you can see in Figure 7.1. To create an event handler for an event, double-click the event name in the event list in the property browser.

An event can provide additional information using parameters. This information varies based on the event being fired . An event that does not contain additional information will always contain two parameters, a System.Object parameter named sender , which is a reference to the object that fired the event, such as a control, and a System.EventArgs parameter named e . The System.EventArgs class is used by events that do not require event data. It is also used as the base class for objects that create custom event arguments which do contain data. Listing 7.2 shows the code for a windows form with event handlers for several different form events.

Listing 7.2 Creating a Handler for a Windows Form's Events
 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace _5_FormEvents {     public class EventForm : System.Windows.Forms.Form     {         private System.ComponentModel.Container components = null;         public EventForm()         {             InitializeComponent();         }         protected override void Dispose( bool disposing )         {             if( disposing )             {                 if(components != null)                 {                     components.Dispose();                 }             }             base.Dispose( disposing );         }         #region Windows Form Designer generated code         private void InitializeComponent()         {             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);             this.ClientSize = new System.Drawing.Size(292, 266);             this.Name = "EventForm";             this.Text = "EventForm";             this.MouseDown += new System.Windows.Forms.MouseEventHandler                 (this.EventForm_MouseDown);             this.Load += new System.EventHandler(this.EventForm_Load);         }         #endregion         private void EventForm_Load(object sender, System.EventArgs e)         {             Console.WriteLine( "Form Loaded: {0}", e.ToString() );         }         private void EventForm_MouseDown(object sender,             System.Windows.Forms.MouseEventArgs e)         {             Console.WriteLine( "Mouse Down - Button: {0} X: {1} Y: {2}",                 e.Button.ToString(), e.X, e.Y );         }     } } 

Comments

We believe you can never have too much information. Fortunately, Windows Forms fire quite a few events, allowing you to know exactly what is going on at any certain moment. Additionally, the events that are fired are not only the result of user interaction but also include events that are fired when certain properties change, such as a system color or font.

Explaining each and every one of the possible events a form can fire would, of course, be superfluous, a restatement of documentation that already exists. Therefore, this recipe focuses on handling form events in the general sense rather than each event in its entirety.

The key to understanding and using Windows Forms events lies in each event handler's associated parameters. It is through the parameters that you know whether there is additional information and whether you can control the outcome of the event using that parameter. Some events merely act to indicate that something has occurred. In Listing 7.2 you can see the event handler for the Load event. This event handler has two parameters, a System.Object type, which all events handlers contain, and a generic EventArgs parameter. When an event handler contains an EventArgs parameter, you will be unable to glean additional information about the event because it doesn't make sense to provide any more information. In the Load event, the only bit of information you know is that the form is being loaded.

Events that provide additional information pass a data type derived from the EventArgs class. This data type contains any additional information for that form. For instance, events that fire as a result of user interaction using the mouse might pass a MouseEventArgs object. This object contains information such as the X and Y coordinates of the event relative to the upper-left corner of the form, the button on the mouse that was clicked, the number of clicks that occurred, and how many detents or notches the mouse wheel rotated . In Listing 7.2, you can see event handlers for several mouse- related events. Some of these events, such as MouseEnter and MouseLeave , do not contain additional information. If you want to know the coordinates of the mouse when the mouse enters or leaves , you can create an event handler for the MouseMove event. The MouseMove as well as the MouseUp and MouseDown events pass a MouseEventArgs parameter to provide additional information. Figure 7.3 shows the results of running the application in Listing 7.2. To avoid a constant flow of information, it does not handle the MouseMove event.

Figure 7.3. Some Windows Forms events can contain additional information about the event details.

graphics/07fig03.gif

Event-handler arguments are not exclusively for providing additional information. Event arguments can also control the outcome of a certain event. For instance, the Closing event is fired when a form is about to be closed. It is still visible, however, and you can create an event handler to cancel the closing process. To create an event handler for the Closing event, set the Cancel property of the CancelEventArgs object passed in as an event parameter to false . Doing so then prevents the form from closing. An event handler for the Closing event follows :

 
 private void EventForm_Closing(object sender, System.ComponentModel.CancelEventArgs e) {     if( MessageBox.Show("Are you sure you want to quit?","Quit?",         MessageBoxButtons.YesNo) == DialogResult.No)     {         e.Cancel = true;     }     else     {         e.Cancel = false;      } } 
 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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