Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

9.4 Events

Visual Studio .NET 2003

Events are used to notify code of a particular occurrence. An event declaration consists of an identifier, either a delegate type or a parameter list, and an optional Implements clause. If a delegate type is specified, the delegate type may not have a return type. If a parameter list is specified, it may not contain Optional or ParamArray parameters. The accessibility domain of the parameter types and/or delegate type must be the same as, or a superset of, the accessibility domain of the event itself. Events may be shared by specifying the Shared modifier.

In addition to the member name added to the type's declaration space, an event declaration implicitly declares several other members. Given an event named X, the following members are added to the declaration space:

  • If the form of the declaration is a method declaration, a nested delegate class named XEventHandler is introduced. The nested delegate class matches the method declaration and has the same accessibility as the event. The attributes in the parameter list apply to the parameters of the delegate class.
  • A Private instance variable typed as the delegate, named XEvent.
  • A method named add_X, which takes the delegate type and has the same access type as the event.
  • A method named remove_X, which takes the delegate type and has the same access type as the event.

If a type attempts to declare a name that matches one of the above names, a compile-time error will result. It is not possible to override or overload any of the introduced members, although it is possible to shadow them in derived types. For example, the class declaration

Class Raiser
    Public Event Constructed(ByVal i As Integer)
End Class

is equivalent to the following declaration

Class Raiser
    Public Delegate Sub ConstructedEventHandler(ByVal i As Integer)

    Protected ConstructedEvent As ConstructedEventHandler

    Public Sub add_Constructed(ByVal d As ConstructedEventHandler)
        ConstructedEvent = _
            CType( _
                ConstructedEvent.Combine(ConstructedEvent, d), _
                    Raiser.ConstructedEventHandler)
    End Sub

    Public Sub remove_Constructed(ByVal d As ConstructedEventHandler)
        ConstructedEvent = _
            CType( _
                ConstructedEvent.Remove(ConstructedEvent, d), _
                    Raiser.ConstructedEventHandler)
    End Sub
End Class

Declaring an event without specifying a delegate type is the simplest and most compact syntax, but has the disadvantage of declaring a new delegate type for each event. For example, in the following example, three hidden delegate types are created, even though all three events have the same parameter list:

Public Class Button
    Public Event Click(sender As Object, e As System.EventArgs)
    Public Event DoubleClick(sender As Object, e As System.EventArgs)
    Public Event RightClick(sender As Object, e As System.EventArgs)
End Class

In the following example, the events simply use the same delegate, EventHandler:

Delegate Sub EventHandler(sender As Object, e As System.EventArgs)
Public Class Button
    Public Event Click As EventHandler
    Public Event DoubleClick As EventHandler
    Public Event RightClick As EventHandler
End Class

Events can be handled in one of two ways: statically or dynamically. Statically handling events is simpler and only requires a WithEvents variable and a Handles clause. In the following example, class Form1 statically handles the event Click of object Button:

Imports System

Public Class Form1
    Public WithEvents Button1 As Button = New Button()
    Public Sub Button1_Click(sender As Object, e As System.EventArgs) _
           Handles Button1.Click
        Console.WriteLine("Button1 was clicked!")
    End Sub
End Class

Dynamically handling events is more complex because the event must be explicitly connected and disconnected to in code. The statement AddHandler adds a handler for an event, and the statement RemoveHandler removes a handler for an event. The next example shows a class Form1 that adds Button1_Click as an event handler for Button1's Click event:

Imports System

Public Class Form1
    Public Sub New()
        ' Add Button1_Click as an event handler for Button1's Click event.
        AddHandler Button1.Click , AddressOf Button1_Click
    End Sub 

    Private Button1 As Button = New Button()

    Sub Button1_Click(sender As Object, e As EventArgs)
        Console.WriteLine("Button1 was clicked!")
    End Sub

    Public Sub Disconnect()
        RemoveHandler Button1.Click, AddressOf Button1_Click
    End Sub 
End Class 

In method Disconnect, the event handler is removed.

EventMemberDeclaration ::=
   [ Attributes ] [ EventModifiers+ ] Event Identifier ParametersOrType
      [ ImplementsClause ] LineTerminator
ParametersOrType ::=
   [ ( [ ParameterList ] ) ] |
   As TypeName
EventModifiers ::= AccessModifier | Shadows | Shared

See Also

9.1 Interface Method Implementation | 9.2 Methods | 9.3 Constructors | 9.5 Constants | 9.6 Instance and Shared Variables | 9.7 Properties | 9.2.5 Method Parameters | 9.2.6 Event Handling | 4.6 Accessibility | 4.3.3 Shadowing | 9.2.4 Shared Methods | Event Statement (Visual Basic Language Reference) | Events and Event Handlers (Visual Basic Language Concepts)

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.