How to handle events in Visual Basic .NET or in Visual Basic 2005 (319823)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual Basic .NET (2003)

This article was previously published under Q319823

SUMMARY

This step-by-step article demonstrates how to connect an event of a control in Visual Basic .NET or in Visual Basic 2005 to the code that will execute when the event occurs. Because Visual Basic is an event-driven language, the ability to execute code when an event occurs is critical.

back to the top

Create the Visual Basic .NET or Visual Basic 2005 Project

  1. Click Start, point to All Programs, point to Microsoft Visual Studio .NET, and then click Microsoft Visual Studio .NET.

    Note In Visual Studio 2005, click Start, point to All Programs, point to Microsoft Visual Studio 2005, and then click 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 Basic Projects under Project Types, click Windows Application under Templates, and then click OK.

    Note In Visual Studio 2005, click Visual Basic under Project Types.
  4. Drag a Button control and a ComboBox control from the toolbox onto the form. By default, Button1 and ComboBox1 are added to the form.
back to the top

Connect Events Through the Visual Studio .NET or Visual Studio 2005 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.vb[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 Button1. SelectedIndexChanged is the default event for the ComboBox control.
  6. To add events other than the default event, use the two drop-down list boxes at the top of the Code window. For example, to add the MouseHover event for Button1, click Button1 in the left drop-down list box, and then click MouseHover in the right drop-down list box. This adds the MouseHover event to the Code window.

    NOTE: You can use these two drop-down list boxes to code any of the events for a form or for controls. You must select the object first in the left drop-down list box and then select the event for that object in the right drop-down list box.
  7. To locate any of the events that are already written, 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. You can click an event in this 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 an OnDropDown event for the ComboBox control:
        Private Sub ComboBox1_DropDown(ByVal sender As Object, ByVal e As System.EventArgs)
              'Insert the handler code here.
        End Sub
    						
    NOTE: The event handler must specify the correct arguments for the event that is being handled (in this case, "ByVal sender As Object, ByVal e As System.EventArgs"). For more information about the arguments for each specific event, see the Visual Studio .NET or Visual Studio 2005 Help documentation for the event.
  2. After the Sub procedure is created, you must hook the Sub procedure to the event. At the end of the function declaration that you added in step 1, type Handles ComboBox1.. This displays a list of all of the possible events for the ComboBox control. Double-click the DropDown event. The resulting code appears as follows:
        Private Sub ComboBox1_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDown
              'Insert the handler code here.
        End Sub
    						
    NOTE: The name of the Sub procedure can be anything. The Handles keyword is what links the Sub procedure to the event. The name of the Sub procedure is independent.
  3. One Sub procedure can handle multiple events. These events can be for the same control or for different controls. The Sub procedure that handles the DropDown event for the ComboBox can also handle the Validate event for the Button.

    Change the name of the Sub procedure to "GenericHandler". (This is not required, but it is a good practice.) After "Handles ComboBox1.DropDown", type , Button1.. This displays a list of all of the events for the button. Double-click Validated. The resulting code appears as follows:
        Private Sub GenericHandler(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.DropDown, Button1.Validated
              'Insert the handler code here.
        End Sub
    					
back to the top

REFERENCES

For more information about how to handle events, visit the following MSDN Web site:

back to the top


Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbHOWTOmaster KB319823 kbAudDeveloper