How to handle events in Visual C# (322685)



The information in this article applies to:

  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# 2005, Express Edition

This article was previously published under Q322685

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

  1. 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.
  2. Click the Form1.cs[Design] tab to return to Design view.
  3. Double-click button1 again. Notice that you return to the Click event that was created in step 1.
  4. Return to Design view.
  5. 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.
  6. To add events other than the default event, follow these steps:

    1. Return to Design view. To do this, click the Form1.cs[Design] tab.
    2. Click button1.
    3. In the Properties window, click Events (which appears as a lightning bolt button at the top of the window).
    4. 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.
    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.
  7. To locate any of the events that are already written, follow these steps:
    1. Return to the Code window. To do this, click the Form1.cs tab.
    2. 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.
    3. Click an event in the right list to position the cursor in that event.
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.
  1. 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, "object sender, System.EventArgs e"). For more information about the arguments for each specific event, see the Visual Studio .NET Help documentation for the event.
  2. After the procedure is created, you must hook the procedure to the event. To do this, follow these steps:
    1. Expand the "Windows Form Designer generated code" section, and then locate the following comments:
      	// 
      	// comboBox1
      	// 
      						
    2. 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.
  3. 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:
    1. Expand the "Windows Form Designer generated code" section, and then locate the following comments:
      	// 
      	// button1
      	// 
      						
    2. 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.
    3. Double-click Validated, and then type the rest of the line as follows:
      this.button1.Validated += new System.EventHandler(this.GenericHandler);
      						
back to the top

REFERENCES

For additional information about how to handle events, visit the following Microsoft Developer Network (MSDN) Web site: back to the top

Modification Type:MajorLast Reviewed:12/29/2005
Keywords:kbHOWTOmaster KB322685 kbAudDeveloper