Events and Event Handling for Visual Basic 6.0 Users

If you are familiar with events and event handling in Visual Basic 6.0, the event model in Visual Basic 2008 may at first seem confusing, but ultimately it is simpler and much more powerful.

Conceptual Differences

In Visual Basic 6.0, events are tied to specific objects and have their own event-handling code. For example, on a form with a button and a menu, each has its own Click event; you have to write code in the event handler for each, even if they both perform exactly the same function.

' Visual Basic 6.0 
Private Sub HelpButton_Click()
    HelpButton.Caption = "Help me!"
End Sub
Private Sub HelpMenu_Click()
    HelpMenu.Caption = "Help me!"
End Sub

In Visual Basic 2008, events are tied to event handlers by means of delegates, allowing you to create a single event handler for multiple objects.

PrivateSub HelpButton_Click(ByVal sender AsObject, ByVal e As _
System.EventArgs) Handles HelpButton.Click, HelpMenu.Click
    sender.Text = "Help me!"EndSub

In the above example, notice that the event declaration has a Handles clause that defines which events will be handled—in this case, the Click events for both the HelpButton and HelpMenu objects. The objects and events do not have to be of the same type; for example you might have a single event handler that handles the Click event of a button, the DoubleClick event of a text box, and the Tick event of a timer.

Notice also that the event declaration contains two parameters: ByVal sender As Object and ByVal e As System.EventArgs. The first parameter, sender, provides a reference to the object that raised the event. The second parameter, e, passes an object specific to the event that is being handled. By referencing the object's properties (and, sometimes, its methods), you can obtain information such as the location of the mouse for mouse events, or data being transferred in drag-and-drop events.

In the following example, the MouseDown event handler uses the sender parameter to determine the type of object that generated the event, and if the object is a PictureBox, the e parameter is used to move a label to the location where the click took place. To duplicate this example, add two PictureBox controls and a Label control to a form.

PrivateSub Form1_MouseDown(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.MouseEventArgs) HandlesMe.MouseDown, _
PictureBox1.MouseDown, PictureBox2.MouseDown

    IfTypeOf sender Is PictureBox Then
        Label1.Location = sender.Location + e.Location
    Else
        MsgBox("Please click a picture")
    EndIfEndSub

Typically, each event produces an event handler with a different event-object type for the second parameter. Some event handlers, such as those for the MouseDown and MouseUp events, have the same object type (MouseEventArgs) for their second parameter. For these types of events, you can use the same event handler to handle both events.

For events that pass different event object types, you must create separate event handlers. For example, the TextChanged event of a TextBox control passes the generic EventArgs event object, and the MouseDown event passes the more specialized MouseEventArgs event object. The MouseEventArgs object contains properties that are specific to mouse events such as Button, to determine which mouse button was pressed; these properties do not apply to a TextBox control and would cause an error if you tried to reference them.

In addition to the conceptual differences in event handling, the names and behavior of some events for various objects are different in Visual Basic 2008. For more information, see Windows Forms Controls for Visual Basic 6.0 Users.

See Also

Concepts

Event Handlers Overview (Windows Forms)

Other Resources

Creating Event Handlers in Windows Forms