8.3 Creating a Button and Handling Events

 <  Day Day Up  >  

You want to create a button and perform an action when the user clicks the button.


Technique

Drag and drop a button control onto your Windows Form within the Windows Forms designer. Alter the appearance of the button as necessary by changing the button's properties. There are two options available when creating an event handler for the Click event. Because the Click event is the most commonly used event that is handled for a button, you can double-click on the button itself and the form designer generates the necessary event-handling method within your class and opens the source code file, placing the cursor at the Click event-handler method body. The second option available when creating an event handler is to select the button within the form designer and click on the lightning bolt toolbar icon within the property browser. The property browser then lists all the possible events that the Button control can fire. Double-click the field next to the name of the event to create an event handler for that event, or type in the name of the event handler in that same field and press the Enter key.

Comments

Just as there are numerous properties for each Windows Form control, there are almost an equal number of events that you can create event handlers for. Furthermore, in addition to visually changing a control's property using the property browser, you can also create an event handler using that same tool window.

Because there are so many events associated with any given control, keeping track of what each event does is obviously a nontrivial task. Thankfully, when you single-click on an event name within the property browser, a small help string appears at the bottom of the property browser, giving you information on when that event will be fired . For obvious tasks such as adding the Click event to a button control, there is little value in that small help string, but you'll soon find yourself referring to it for the more obscure events such as the ChangeUICues event.

Windows Forms controls event handlers use two parameters. The first parameter, which is the same regardless of the control, is a System.Object parameter named sender that allows you to access the control that fired the event. In most cases, you don't need to use that parameter because you already know what control fired the event and more than likely already have a private member variable associated with the control. The next parameter is a collection of event-handler arguments, which are different based on the control and the event being handled. The rule of thumb for this parameter is that if the parameter data type is System.EventArgs , then there is no additional information available for that event. For instance, if you create an event handler for the MouseHover event, you might expect the event arguments to give you the current location of the mouse cursor relative to that control. Unfortunately, because the second parameter of that MouseHover event handler is System.EventArgs , there is no such information available. In such cases, you have to either call any necessary methods to determine that information or use other event handlers for events which do provide that information. In the case of the MouseHover event, you can also create an event handler for the MouseMove event, saving the mouse coordinates specified in the MouseEventArgs parameter of that event handler.

If you look under the hood at the code that is generated when creating an event handler, you'll see that control events (as well as events for the form itself) use multicast delegates as the main event-handling mechanism. Listing 8.3 creates a form with two buttons . Each button's Click event is associated with an event handler. You make this association using the addition assignment ( += ) operator on the Click property of each button. You can in fact programmatically associate multiple event handlers for a single event using this method as well as dynamically add or remove event handlers during application runtime.

Listing 8.3 Associating Events with an Event Handler Using the Multicast Delegate Syntax
 private void InitializeComponent() {     this.sayHello = new System.Windows.Forms.Button();     this.closeForm = new System.Windows.Forms.Button();     this.SuspendLayout();     //     // sayHello     //     this.sayHello.Location = new System.Drawing.Point(98, 16);     this.sayHello.Name = "sayHello";     this.sayHello.Size = new System.Drawing.Size(96, 23);     this.sayHello.TabIndex = 0;     this.sayHello.Text = "Say Hello";     // event handler association for the Click event     this.sayHello.Click += new System.EventHandler(this.sayHello_Click);     //     // closeForm     //     this.closeForm.Location = new System.Drawing.Point(98, 64);     this.closeForm.Name = "closeForm";     this.closeForm.Size = new System.Drawing.Size(96, 23);     this.closeForm.TabIndex = 1;     this.closeForm.Text = "Close Form";     this.closeForm.Click += new System.EventHandler(this.closeForm_Click);     //     // Form1     //     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);     this.ClientSize = new System.Drawing.Size(292, 118);     this.Controls.Add(this.closeForm);     this.Controls.Add(this.sayHello);     this.Name = "Form1";     this.Text = "Form1";     this.ResumeLayout(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