Microsoft KB Archive/322685

= How to handle events in Visual C# =

Article ID: 322685

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-



This article was previously published under Q322685



For a Microsoft Visual Basic .NET version of this article, see 319823.

IN THIS TASK
SUMMARY
 * Create the Visual C# .NET Project
 * Connect Events Through the Visual Studio .NET IDE
 * Connect Events Programmatically

REFERENCES



SUMMARY
This step-by-step article demonstrates how to connect an event of a control in Visual C# to the code that will execute when the event occurs.

back to the top

Create the Visual C# project

 * 1) Click Start, point to All Programs, point to Microsoft Visual Studio .NET or Microsoft Visual Studio 2005, and then click Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
 * 2) On the File menu, point to New, and then click Project.
 * 3) In the New Project dialog box, click Visual C# Projects under Project Types, click Windows Application under Templates, and then click OK.

Note In Visual Studio 2005, click Visual C# under Project Types.
 * 1) Drag a Button control and a ComboBox control from the toolbox to the form. By default, button1 and comboBox1 are added to the form.

back to the top

Connect events through the Visual Studio .NET IDE
 Double-click button1 to open the code-behind file and to add the code for the default event of the control. The Click event is the default event of the Button control.

NOTE: If you double-click an object on a form in Design view, a method call is generated for and connected to the default event automatically. The Code window opens to that method. Click the Form1.cs[Design] tab to return to Design view. Double-click button1 again. Notice that you return to the Click event that was created in step 1. Return to Design view. Double-click comboBox1 to open the code-behind file and to add the code for the default event of the comboBox1 control. Notice that the SelectedIndexChanged event that is connected to the ComboBox is a different event than the one that was created for the button1 control. SelectedIndexChanged is the default event for the ComboBox control. To add events other than the default event, follow these steps:

 Return to Design view. To do this, click the Form1.cs[Design] tab.</li> Click button1.</li> In the Properties window, click Events (which appears as a lightning bolt button at the top of the window).</li> To add the MouseHover event for button1 to the Code window and to connect the MouseHover event to the handler, double-click MouseHover in the list.</li></ol>

NOTE: You can use the Events view of the Properties window to code any of the events for a form or for controls. You must first select the object on the form and then select the event for that object in the Event view of the Properties window.

NOTE: To connect the event to a procedure with the default name, you can double-click the event. To connect to a function that has a name other than the default, type a name for the event function, or select a function that already exists and that matches the signature that is required for that event from the list.</li> To locate any of the events that are already written, follow these steps: <ol style="list-style-type: lower-alpha;"> Return to the Code window. To do this, click the Form1.cs tab.</li> Click the top-level item (which is the name of your form) in the left drop-down list box. Notice that the right drop-down list box lists all of the events that are coded for any object on that form.</li> Click an event in the right list to position the cursor in that event.</li></ol> </li></ol>

back to the top

Connect events programmatically
NOTE: This section demonstrates how to make a method handle an event. This section does not demonstrate how to connect events dynamically. <ol>  Add the following code to the Form class to add a DropDown event for the ComboBox control: private void GenericHandler(object sender, System.EventArgs e)   { // Insert the handler code here. } NOTE: The event handler must specify the correct arguments for the event that is being handled (in this case, &quot;object sender, System.EventArgs e&quot;). For more information about the arguments for each specific event, see the Visual Studio .NET Help documentation for the event. </li> After the procedure is created, you must hook the procedure to the event. To do this, follow these steps: <ol style="list-style-type: lower-alpha;">  Expand the &quot;Windows Form Designer generated code&quot; section, and then locate the following comments: //    // comboBox1 //                        </li>  After the following line, which hooks the SelectItemChanged event this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); add the following code: this.comboBox1.DropDown += new System.EventHandler(this.GenericHandler); NOTE: The name of the procedure can be anything. You link the procedure to the event by adding the EventHandler delegate. The name of the procedure is independent of the process to link the event to the procedure. However, if you change the name of the handler, you must modify this code to reflect the new name. If you do not modify the code, the event is not handled because a handler no longer exists. </li></ol> </li> One procedure can handle multiple events. These events can be for the same control or for different controls. The procedure that handles the DropDown event for the ComboBox can also handle the Validate event for the Button.

To display a list of all of the events, the methods, and the properties for a control, follow these steps: <ol style="list-style-type: lower-alpha;">  Expand the &quot;Windows Form Designer generated code&quot; section, and then locate the following comments: //    // button1 //                        </li>  After the following line of code this.button1.Click += new System.EventHandler(this.button1_Click); type the following code: this.button1. Notice that a list appears with all of the events, the methods, and the properties for the button1 control. </li>  Double-click Validated, and then type the rest of the line as follows: this.button1.Validated += new System.EventHandler(this.GenericHandler); </li></ol> </li></ol>

back to the top

<div class="references_section">