1. Home
  2. Computing & Technology
  3. Visual Basic
Working With Printers
But wait! There's more!

If you add another button to the form, and modify the code as explained in the lesson for chapter 6, you can trigger the event code from either button.

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Event MyEvent()
    Private Sub Form1_Load( _
	ByVal sender As System.Object, _
	ByVal e As System.EventArgs) _
        Handles MyBase.Load
        AddHandler _
            Form1.MyEvent, AddressOf MyEventHandler
    End Sub
    Private Sub Button_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Button1.Click, Button2.Click
        Debug.WriteLine( _
            "Either Button1 or " & vbcrlf & _
	    "Button 2 was clicked and ...")
        RaiseEvent MyEvent()
    End Sub
    Sub MyEventHandler()
        Debug.WriteLine( _
            "MyEventHandler caught the event")
    End Sub
End Class

Sharp eyed programmers may notice that in this example, you could just move the code in MyEventHandler to the Button_Click subroutine and get the same result ... but I'm making a point here. Suppose you wanted to have a normal button event handler also work as an event handler for your code? The main difference is that you now have to pass parameters again because the normal button event handler has them and you have to match that code. To demonstrate this, add a third button and change the code to match this example:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Event MyEvent( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs)
    Private Sub Form1_Load( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load
        AddHandler _
            Form1.MyEvent, AddressOf MyEventHandler
    End Sub
    Sub MyEventHandler( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Button1.Click, Button2.Click
        Debug.WriteLine( _
            "Either Button1, Button2" & vbCrLf & _
            "OR Button3 was clicked and ..." & _
            vbCrLf & "MyEventHandler caught the event")
    End Sub
    Private Sub Button3_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Button3.Click
        Debug.WriteLine( _
            "Button3 clicked, MyEvent raised and ...")
        RaiseEvent MyEvent(sender, e)
    End Sub
End Class

Note that you get a slightly different result in the Output window when Button3 is clicked now.

By now, you should be seeing that the Handles clause and the AddHandler statement have a lot in common! But the AddHandler statement can associate more than one event handler with the same event (the Handles clause can't do that). Here's what this looks like in code.

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Event MyEvent( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs)
    Private Sub Form1_Load( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load
        AddHandler _
            Form1.MyEvent, AddressOf MyEventHandler
        AddHandler _
            Form1.MyEvent, AddressOf MyOtherHandler
    End Sub
    Sub MyEventHandler( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Button1.Click, Button2.Click
        Debug.WriteLine( _
            "Either Button1, Button2" & vbCrLf & _
            "OR Button3 was clicked and ..." & _
            vbCrLf & "MyEventHandler caught the event")
    End Sub
    Sub MyOtherHandler( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) 
        Debug.WriteLine( _
            "And MyOtherHandler also called" & vbCrLf & _
            "whenever the event is raised!")
    End Sub
    Private Sub Button3_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Button3.Click
        Debug.WriteLine( _
            "Button3 clicked, MyEvent raised and ...")
        RaiseEvent MyEvent(sender, e)
    End Sub
End Class

The Microsoft documentation for the AddHandler statement is similar to this example except that the event handler subroutine is coded as part of an independent VB.NET class. This adds some additional complexity (that's why I didn't code these examples that way) but also provides more flexibility.

Next page >>>   More on Printing!
Page 1, 2, 3, 4

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2008 About.com, a part of The New York Times Company.

All rights reserved.