Microsoft KB Archive/319823

= How to handle events in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 319823

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q319823



For a Microsoft Visual C# .NET version of this article, see 322685.

IN THIS TASK
SUMMARY
 * Create the Visual Basic .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 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.
 * 1) On the File menu, point to New, and then click Project.
 * 2) 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.
 * 1) 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.
 * 1) Click the Form1.vb[Design] tab to return to Design view.
 * 2) Double-click Button1 again. Notice that you return to the Click event that was created in step 1.
 * 3) Return to Design view.
 * 4) 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.
 * 5) 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.
 * 1) 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.   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, &quot;ByVal sender As Object, ByVal e As System.EventArgs&quot;). For more information about the arguments for each specific event, see the Visual Studio .NET or Visual Studio 2005 Help documentation for the event.   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.   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 &quot;GenericHandler&quot;. (This is not required, but it is a good practice.) After &quot;Handles ComboBox1.DropDown&quot;, 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

