Export (0) Print
Expand All
Expand Minimize
9 out of 20 rated this helpful - Rate this topic

EventHandler Generic Delegate

Note: This delegate is new in the .NET Framework version 2.0.

Represents the method that will handle an event. The generic type argument specifies the type of the event data generated by the event.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

'Declaration
<SerializableAttribute> _
Public Delegate Sub EventHandler(Of TEventArgs As EventArgs) ( _
	sender As Object, _
	e As TEventArgs _
)
'Usage
Dim instance As New EventHandler(Of TEventArgs)(AddressOf HandlerMethod)
J# supports the use of generic types and methods, but not the declaration of new ones.
JScript does not support generic types and methods.

Parameters

sender

The source of the event.

e

An EventArgs that contains the event data.

The event model in the .NET Framework is based on having an event delegate that connects an event with its handler. To raise an event, two elements are needed:

  • A delegate that refers to a method that provides the response to the event.

  • A class that holds the event data.

The delegate is a type that defines a signature, that is, the return value type and parameter list types for a method. You can use the delegate type to declare a variable that can refer to any method with the same signature as the delegate.

The standard signature of an event handler delegate defines a method that does not return a value, whose first parameter is of type Object and refers to the instance that raises the event, and whose second parameter is derived from type EventArgs and holds the event data. If the event does not generate event data, the second parameter is simply an instance of EventArgs. Otherwise, the second parameter is a custom type derived from EventArgs and supplies any fields or properties needed to hold the event data.

EventHandler is a predefined delegate that represents an event handler method for an event, regardless of whether the event generates event data. If your event does not generate event data, substitute EventArgs for the generic type parameter; otherwise, supply your own custom event data type and substitute it for the generic type parameter.

The advantage of using EventHandler is that you do not need to code your own custom delegate if your event generates event data. Additionally, the .NET Framework needs only one implementation to support EventHandler regardless of the event data type you substitute for the generic type parameter.

To associate the event with the method that will handle the event, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.

For more information about event handler delegates, see Events and Delegates.

The following code example declares event data and a generic EventHandler delegate that uses the event data, and shows how the event is raised.

' This example demonstrates the EventHandler(Of T) delegate.
Imports System
Imports System.Collections.Generic

'---------------------------------------------------------
Public Class MyEventArgs
   Inherits EventArgs
   Private msg As String
   
   Public Sub New(messageData As String)
      msg = messageData
   End Sub 'New
   
   Public Property Message() As String
      Get
         Return msg
      End Get
      Set
         msg = value
      End Set
   End Property
End Class 'MyEventArgs 
'---------------------------------------------------------
Public Class HasEvent
   ' Declare an event of delegate type EventHandler of 
   ' MyEventArgs.
   Public Event SampleEvent As EventHandler(Of MyEventArgs)
   
   Public Sub DemoEvent(val As String)
      RaiseEvent SampleEvent(Me, New MyEventArgs(val))
   End Sub 'DemoEvent
End Class 'HasEvent 
'---------------------------------------------------------
Public Class Sample
   Public Shared Sub Main()
      Dim he As New HasEvent()
      AddHandler he.SampleEvent, AddressOf SampleEventHandler
      he.DemoEvent("Hey there, Bruce!")
      he.DemoEvent("How are you today?")
      he.DemoEvent("I'm pretty good.")
      he.DemoEvent("Thanks for asking!")
   End Sub 'Main

   Private Shared Sub SampleEventHandler(src As Object, _
                                         mea As MyEventArgs)
      Console.WriteLine(mea.Message)
   End Sub 'SampleEventHandler
End Class 'Sample
'---------------------------------------------------------
'
'This example produces the following results:
'
'Hey there, Bruce!
'How are you today?
'I'm pretty good.
'Thanks for asking!
'

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.