VB.Net Tutorial/Event/Event — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 16:40, 26 мая 2010

Add and remove event handler

Imports System.Windows.Forms
public class AddRemoveEventHandler
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked Then
            AddHandler Button1.Click, AddressOf ButtonHandler1
        Else
            RemoveHandler Button1.Click, AddressOf ButtonHandler1
        End If
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        If RadioButton2.Checked Then
            AddHandler Button1.Click, AddressOf ButtonHandler2
        Else
            RemoveHandler Button1.Click, AddressOf ButtonHandler2
        End If
    End Sub
    Private Sub ButtonHandler1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MessageBox.Show("ButtonHandler1")
    End Sub
    Private Sub ButtonHandler2(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MessageBox.Show("ButtonHandler2")
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RadioButton1.Checked = True
    End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.RadioButton1 = New System.Windows.Forms.RadioButton
        Me.RadioButton2 = New System.Windows.Forms.RadioButton
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        "
        "RadioButton1
        "
        Me.RadioButton1.AutoSize = True
        Me.RadioButton1.Location = New System.Drawing.Point(8, 8)
        Me.RadioButton1.Name = "RadioButton1"
        Me.RadioButton1.Size = New System.Drawing.Size(99, 17)
        Me.RadioButton1.TabIndex = 0
        Me.RadioButton1.Text = "ButtonHandler1"
        Me.RadioButton1.UseVisualStyleBackColor = True
        "
        "RadioButton2
        "
        Me.RadioButton2.AutoSize = True
        Me.RadioButton2.Location = New System.Drawing.Point(8, 40)
        Me.RadioButton2.Name = "RadioButton2"
        Me.RadioButton2.Size = New System.Drawing.Size(99, 17)
        Me.RadioButton2.TabIndex = 1
        Me.RadioButton2.Text = "ButtonHandler2"
        Me.RadioButton2.UseVisualStyleBackColor = True
        "
        "Button1
        "
        Me.Button1.Location = New System.Drawing.Point(152, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Button1"
        Me.Button1.UseVisualStyleBackColor = True
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.RadioButton2)
        Me.Controls.Add(Me.RadioButton1)
        Me.Name = "Form1"
        Me.Text = "AddRemoveHandler"
        Me.ResumeLayout(False)
        Me.PerformLayout()
    End Sub
    Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton
    Friend WithEvents Button1 As System.Windows.Forms.Button
End Class

Add, remove event handler to/from ToggleButton

Imports System.Windows.Forms
public class AddHandlerAndRemoveHandlerForToggleButton
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.CheckBox1 = New System.Windows.Forms.CheckBox
        Me.SuspendLayout()
        "
        "Button1
        "
        Me.Button1.Location = New System.Drawing.Point(40, 32)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Click Me!"
        "
        "CheckBox1
        "
        Me.CheckBox1.Appearance = System.Windows.Forms.Appearance.Button
        Me.CheckBox1.Checked = True
        Me.CheckBox1.CheckState = System.Windows.Forms.CheckState.Checked
        Me.CheckBox1.Location = New System.Drawing.Point(136, 31)
        Me.CheckBox1.Name = "CheckBox1"
        Me.CheckBox1.Size = New System.Drawing.Size(128, 24)
        Me.CheckBox1.TabIndex = 1
        Me.CheckBox1.Text = "Toggle Event Handling"
        Me.CheckBox1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.CheckBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub CheckBox1_CheckedChanged( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked Then
            AddHandler Button1.Click, _
                AddressOf myClickHandler
        Else
            RemoveHandler Button1.Click, _
                AddressOf myClickHandler
        End If
    End Sub
    Private Sub myClickHandler( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs)
        MsgBox("Event Handled!")
    End Sub
End Class

Custom event handler

Imports System.Collections
Delegate Sub NameChangedDelegate(ByVal new_name As String)
public class Test
   Private WithEvents emp As Employee
   public Shared Sub Main
        Dim emp As New Employee
        AddHandler emp.NameChanged, AddressOf Employee_NameChanged
        AddHandler emp.NameChanged, AddressOf Employee_NameChanged
        emp.FirstName = "Rod"
        RemoveHandler emp.NameChanged, AddressOf Employee_NameChanged
        emp.LastName = "Stephens"
        RemoveHandler emp.NameChanged, AddressOf Employee_NameChanged
   End Sub
    Private Shared Sub Employee_NameChanged(ByVal new_name As String) Handles emp.NameChanged
        Console.WriteLine("Employee_NameChanged: " & new_name)
    End Sub
End class
Public Class Employee
    Private m_FirstName As String
    Public Property FirstName() As String
        Get
            Return m_FirstName
        End Get
        Set(ByVal value As String)
            m_FirstName = value
            RaiseEvent NameChanged(m_FirstName & " " & m_LastName)
        End Set
    End Property
    Private m_LastName As String
    Public Property LastName() As String
        Get
            Return m_LastName
        End Get
        Set(ByVal value As String)
            m_LastName = value
            RaiseEvent NameChanged(m_FirstName & " " & m_LastName)
        End Set
    End Property
    Private m_EventDelegates As New ArrayList
    Public Delegate Sub NameChangedDelegate(ByVal new_name As String)
    Public Custom Event NameChanged As NameChangedDelegate
        AddHandler(ByVal value As NameChangedDelegate)
            Console.WriteLine("AddHandler")
            m_EventDelegates.Add(value)
        End AddHandler
        RemoveHandler(ByVal value As NameChangedDelegate)
            Console.WriteLine("RemoveHandler")
            m_EventDelegates.Remove(value)
        End RemoveHandler
        RaiseEvent(ByVal new_name As String)
            Console.WriteLine("RaiseEvent (" & new_name & ")")
            For Each a_delegate As NameChangedDelegate In m_EventDelegates
                a_delegate(new_name.Replace(" ", "+"))
            Next a_delegate
        End RaiseEvent
    End Event
End Class
AddHandler
AddHandler
RaiseEvent (Rod )
Employee_NameChanged: Rod+
Employee_NameChanged: Rod+
RemoveHandler
RaiseEvent (Rod Stephens)
Employee_NameChanged: Rod+Stephens
RemoveHandler

Event handler

Module Module1
    Class Job
        Public Event Meeting()
        Public Event Testing(ByVal Temp As Integer)
        Public Event Coding(ByVal Room As String, ByVal Duration As Integer)
        Public Sub GenerateEvents()
            RaiseEvent Meeting()
            RaiseEvent Testing(212)
            RaiseEvent Coding("VB.net", 25)
        End Sub
    End Class
    Dim WithEvents Alarms As New Job()
    Sub MyMeeting() Handles Alarms.Meeting
        Console.WriteLine("MyMeeting alarm occurred")
    End Sub
    Sub MyTesting(ByVal Temp As Integer) Handles Alarms.Testing
        Console.WriteLine("MyTesting alarm occurred: Temp: " & Temp)
    End Sub
    Sub MyCoding(ByVal Room As String, ByVal Duration As Integer) Handles Alarms.Coding
        Console.WriteLine("MyCoding alarm occurred: " & Room & " " & Duration)
    End Sub
    Sub Main()
        Alarms.GenerateEvents()
    End Sub
End Module
MyMeeting alarm occurred
MyTesting alarm occurred: Temp: 212
MyCoding alarm occurred: VB.net 25

Get event sender type

Imports System.Windows.Forms
public class TypeOfSenderIsControl
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Friend WithEvents Button6 As System.Windows.Forms.Button
    Friend WithEvents Button7 As System.Windows.Forms.Button
    Friend WithEvents Button8 As System.Windows.Forms.Button
    Friend WithEvents Button9 As System.Windows.Forms.Button
    Friend WithEvents Button10 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.Button4 = New System.Windows.Forms.Button
        Me.Button5 = New System.Windows.Forms.Button
        Me.Button6 = New System.Windows.Forms.Button
        Me.Button7 = New System.Windows.Forms.Button
        Me.Button8 = New System.Windows.Forms.Button
        Me.Button9 = New System.Windows.Forms.Button
        Me.Button10 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        "
        "Button1
        "
        Me.Button1.Location = New System.Drawing.Point(80, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        "
        "Button2
        "
        Me.Button2.Location = New System.Drawing.Point(80, 48)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 1
        Me.Button2.Text = "Button2"
        "
        "Button3
        "
        Me.Button3.Location = New System.Drawing.Point(80, 80)
        Me.Button3.Name = "Button3"
        Me.Button3.TabIndex = 2
        Me.Button3.Text = "Button3"
        "
        "Button4
        "
        Me.Button4.Location = New System.Drawing.Point(80, 112)
        Me.Button4.Name = "Button4"
        Me.Button4.TabIndex = 3
        Me.Button4.Text = "Button4"
        "
        "Button5
        "
        Me.Button5.Location = New System.Drawing.Point(80, 144)
        Me.Button5.Name = "Button5"
        Me.Button5.TabIndex = 4
        Me.Button5.Text = "Button5"
        "
        "Button6
        "
        Me.Button6.Location = New System.Drawing.Point(168, 16)
        Me.Button6.Name = "Button6"
        Me.Button6.TabIndex = 5
        Me.Button6.Text = "Button6"
        "
        "Button7
        "
        Me.Button7.Location = New System.Drawing.Point(168, 48)
        Me.Button7.Name = "Button7"
        Me.Button7.TabIndex = 6
        Me.Button7.Text = "Button7"
        "
        "Button8
        "
        Me.Button8.Location = New System.Drawing.Point(168, 80)
        Me.Button8.Name = "Button8"
        Me.Button8.TabIndex = 7
        Me.Button8.Text = "Button8"
        "
        "Button9
        "
        Me.Button9.Location = New System.Drawing.Point(168, 112)
        Me.Button9.Name = "Button9"
        Me.Button9.TabIndex = 8
        Me.Button9.Text = "Button9"
        "
        "Button10
        "
        Me.Button10.Location = New System.Drawing.Point(168, 144)
        Me.Button10.Name = "Button10"
        Me.Button10.TabIndex = 9
        Me.Button10.Text = "Button10"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Button10)
        Me.Controls.Add(Me.Button9)
        Me.Controls.Add(Me.Button8)
        Me.Controls.Add(Me.Button7)
        Me.Controls.Add(Me.Button6)
        Me.Controls.Add(Me.Button5)
        Me.Controls.Add(Me.Button4)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub Button1_Click( _
             ByVal sender As System.Object, _
             ByVal e As System.EventArgs) _
             Handles Button1.Click
        MsgBox("Hello World!")
    End Sub
    Private Sub LotsOfButtons( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles Button1.Click, Button2.Click, _
                    Button3.Click, Button4.Click, _
                    Button5.Click, Button6.Click, _
                    Button7.Click, Button8.Click, _
                    Button9.Click, Button10.Click
        Dim clickedCtrl As Control
        If TypeOf sender Is Control Then
            clickedCtrl = DirectCast(sender, Control)
            Console.WriteLine(clickedCtrl.Text)
        End If
    End Sub
End Class

Handle events from more than one components

Imports System.Windows.Forms
public class EventHandlerForMoreThanOneComponent
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
    Friend WithEvents txtFirstName As System.Windows.Forms.TextBox
    Friend WithEvents txtLastName As System.Windows.Forms.TextBox
    Friend WithEvents txtEmail As System.Windows.Forms.TextBox
    Friend WithEvents lblFirstName As System.Windows.Forms.Label
    Friend WithEvents lblLastName As System.Windows.Forms.Label
    Friend WithEvents lblEmail As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ruponents = New System.ruponentModel.Container()
        Me.txtFirstName = New System.Windows.Forms.TextBox()
        Me.txtLastName = New System.Windows.Forms.TextBox()
        Me.txtEmail = New System.Windows.Forms.TextBox()
        Me.lblFirstName = New System.Windows.Forms.Label()
        Me.lblLastName = New System.Windows.Forms.Label()
        Me.lblEmail = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        "
        "txtFirstName
        "
        Me.txtFirstName.Location = New System.Drawing.Point(80, 32)
        Me.txtFirstName.Name = "txtFirstName"
        Me.txtFirstName.Size = New System.Drawing.Size(152, 20)
        Me.txtFirstName.TabIndex = 1
        Me.txtFirstName.Text = ""
        "
        "txtLastName
        "
        Me.txtLastName.Location = New System.Drawing.Point(80, 56)
        Me.txtLastName.Name = "txtLastName"
        Me.txtLastName.Size = New System.Drawing.Size(152, 20)
        Me.txtLastName.TabIndex = 2
        Me.txtLastName.Text = ""
        "
        "txtEmail
        "
        Me.txtEmail.Location = New System.Drawing.Point(80, 80)
        Me.txtEmail.Name = "txtEmail"
        Me.txtEmail.Size = New System.Drawing.Size(224, 20)
        Me.txtEmail.TabIndex = 3
        Me.txtEmail.Text = ""
        "
        "lblFirstName
        "
        Me.lblFirstName.AutoSize = True
        Me.lblFirstName.Location = New System.Drawing.Point(8, 32)
        Me.lblFirstName.Name = "lblFirstName"
        Me.lblFirstName.Size = New System.Drawing.Size(59, 13)
        Me.lblFirstName.TabIndex = 4
        Me.lblFirstName.Text = "First Name"
        "
        "lblLastName
        "
        Me.lblLastName.AutoSize = True
        Me.lblLastName.Location = New System.Drawing.Point(8, 56)
        Me.lblLastName.Name = "lblLastName"
        Me.lblLastName.Size = New System.Drawing.Size(59, 13)
        Me.lblLastName.TabIndex = 5
        Me.lblLastName.Text = "Last Name"
        "
        "lblEmail
        "
        Me.lblEmail.AutoSize = True
        Me.lblEmail.Location = New System.Drawing.Point(8, 80)
        Me.lblEmail.Name = "lblEmail"
        Me.lblEmail.Size = New System.Drawing.Size(33, 13)
        Me.lblEmail.TabIndex = 6
        Me.lblEmail.Text = "Email"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(312, 125)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblEmail, Me.lblLastName, Me.lblFirstName, Me.txtEmail, Me.txtLastName, Me.txtFirstName})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub Names_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFirstName.TextChanged, txtLastName.TextChanged
        txtEmail.Text = txtFirstName.Text & "." & txtLastName.Text 
    End Sub
End Class

Inherit Event

Module Module1
    Public Class DailyEvents
        Public Event Meeting(ByVal Item As String,ByVal StartTime As String)
        Public Event Coding(ByVal Item As String,ByVal StartTime As String)
        Public Event Testing(ByVal Item As String,ByVal StartTime As String)
        Public Sub GenerateEvents()
            RaiseEvent Meeting("A", "9:30AM")
            RaiseEvent Meeting("B", "11:30AM")
            RaiseEvent Meeting("C", "2:30PM")
            RaiseEvent Coding("D", "12:30PM")
            RaiseEvent Testing("E", "5:30PM")
        End Sub
    End Class
    Public Class TodaysActivities
        Inherits DailyEvents
        Public StartDay As DateTime
        Public EndDay As DateTime
        Public Sub New(ByVal StartDay As DateTime, ByVal EndDay As DateTime)
            MyBase.New()
            Me.StartDay = StartDay
            Me.EndDay = EndDay
        End Sub
    End Class
    Dim WithEvents Today As New TodaysActivities(Now(), Now.AddHours(8))
    Sub ScheduleHandler(ByVal Item As String,ByVal StartTime As String)
        Console.WriteLine(Item & " at: " & StartTime)
    End Sub
    Sub Main()
        AddHandler Today.Meeting, AddressOf ScheduleHandler
        AddHandler Today.Coding, AddressOf ScheduleHandler
        AddHandler Today.Testing, AddressOf ScheduleHandler
        Today.GenerateEvents()
    End Sub
End Module
A at: 9:30AM
B at: 11:30AM
C at: 2:30PM
D at: 12:30PM
E at: 5:30PM

Multiple Event Handler

Module Module1
    Class DailyJob
        Public Event Coding(ByVal Item As String, ByVal StartTime As DateTime)
        Public Event Testing(ByVal Item As String, ByVal StartTime As DateTime)
        Public Event Meeting(ByVal Item As String, ByVal StartTime As DateTime)
        Public Sub GenerateEvents()
            RaiseEvent Coding("coding", Now())
            RaiseEvent Testing("testing", Now().AddMinutes(5.0))
            RaiseEvent Meeting("meeting", Now.AddMinutes(10.0))
        End Sub
    End Class
    Dim WithEvents ThisDailyJob As New DailyJob()
    Sub DoJob(ByVal Item As String, ByVal StartTime As DateTime)
        Console.WriteLine("Starting " & Item & " at: " & StartTime)
    End Sub
    Sub Main()
        AddHandler ThisDailyJob.Coding, AddressOf DoJob
        AddHandler ThisDailyJob.Testing, AddressOf DoJob
        AddHandler ThisDailyJob.Meeting, AddressOf DoJob
        ThisDailyJob.GenerateEvents()
    End Sub
End Module
Starting coding at: 11/05/2007 9:33:45 PM
Starting testing at: 11/05/2007 9:38:45 PM
Starting meeting at: 11/05/2007 9:43:45 PM

Raise Events

Module Module1
    Dim WithEvents ValueInfo As New Value()
    Class Value
        Public Event ValueUp(ByVal Amount As Double)
        Public Event ValueDown(ByVal Amount As Double)
        Public Event Result(ByVal Amount As Double, ByVal AnnounceDate As DateTime)
        Public Sub GenerateEvents()
            RaiseEvent ValueUp(2)
            RaiseEvent ValueDown(-5.5)
            RaiseEvent Result(1.25, Now())
        End Sub
    End Class
    Sub PriceGoingUp(ByVal Price As Double)
        Console.WriteLine("Up: " & Price)
    End Sub
    Sub PriceGoingDown(ByVal Price As Double)
        Console.WriteLine("Down: " & Price)
    End Sub
    Sub ResultAnnouncement(ByVal Amount As Double, ByVal AnnounceDate As DateTime)
        Console.WriteLine("Result: " & Amount & " " & AnnounceDate)
    End Sub

    Sub Main()
        AddHandler ValueInfo.ValueUp, AddressOf PriceGoingUp
        AddHandler ValueInfo.ValueDown, AddressOf PriceGoingDown
        AddHandler ValueInfo.Result, AddressOf ResultAnnouncement
        ValueInfo.GenerateEvents()
    End Sub

End Module
Up: 2
Down: -5.5
Result: 1.25 11/05/2007 9:33:43 PM

Subclass EventArgs to create your own event arguments

Module Module1
    Class MyEventArgs
        Inherits System.EventArgs
        Public Message As String
        Public Time As DateTime
        Public Sub New(ByVal S As String, ByVal DT As DateTime)
            MyBase.New()
            Message = S
            Time = DT
        End Sub
    End Class
    Class MyMonitor
        Public Event EventStart(ByVal E As Object, ByVal Args As MyEventArgs)
        Public Sub GenerateEvent()
            Dim Args As New MyEventArgs("Hacker, Hacker", Now())
            RaiseEvent EventStart(Me, Args)
        End Sub
    End Class
    Dim WithEvents HackerAlarm As New MyMonitor()
    Sub Attack(ByVal O As Object, ByVal Args As MyEventArgs) Handles HackerAlarm.EventStart
        Console.WriteLine("Hack Attack in progress")
        Console.WriteLine(Args.Message)
        Console.WriteLine(Args.Time)
    End Sub
    Sub Main()
        HackerAlarm.GenerateEvent()
    End Sub
End Module
Hack Attack in progress
Hacker, Hacker
11/05/2007 9:33:47 PM

Use one method to handle more than one RadioButton click event

Imports System.Windows.Forms
public class HandlerMoreThanOneRadioButtonEventWithOneMethod
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim radioButtons(4) As RadioButton
    Dim selectedOption As Integer = 0
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
        radioButtons(0) = RadioButton1
        radioButtons(1) = RadioButton2
        radioButtons(2) = RadioButton3
        radioButtons(3) = RadioButton4
        radioButtons(4) = RadioButton5
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton2 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton3 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton4 As System.Windows.Forms.RadioButton
    Friend WithEvents RadioButton5 As System.Windows.Forms.RadioButton
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.RadioButton1 = New System.Windows.Forms.RadioButton
        Me.RadioButton2 = New System.Windows.Forms.RadioButton
        Me.RadioButton3 = New System.Windows.Forms.RadioButton
        Me.RadioButton4 = New System.Windows.Forms.RadioButton
        Me.RadioButton5 = New System.Windows.Forms.RadioButton
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        "
        "RadioButton1
        "
        Me.RadioButton1.Location = New System.Drawing.Point(118, 64)
        Me.RadioButton1.Name = "RadioButton1"
        Me.RadioButton1.TabIndex = 0
        Me.RadioButton1.Text = "RadioButton1"
        "
        "RadioButton2
        "
        Me.RadioButton2.Location = New System.Drawing.Point(118, 88)
        Me.RadioButton2.Name = "RadioButton2"
        Me.RadioButton2.TabIndex = 1
        Me.RadioButton2.Text = "RadioButton2"
        "
        "RadioButton3
        "
        Me.RadioButton3.Location = New System.Drawing.Point(118, 112)
        Me.RadioButton3.Name = "RadioButton3"
        Me.RadioButton3.TabIndex = 2
        Me.RadioButton3.Text = "RadioButton3"
        "
        "RadioButton4
        "
        Me.RadioButton4.Location = New System.Drawing.Point(118, 136)
        Me.RadioButton4.Name = "RadioButton4"
        Me.RadioButton4.TabIndex = 3
        Me.RadioButton4.Text = "RadioButton4"
        "
        "RadioButton5
        "
        Me.RadioButton5.Location = New System.Drawing.Point(118, 160)
        Me.RadioButton5.Name = "RadioButton5"
        Me.RadioButton5.TabIndex = 4
        Me.RadioButton5.Text = "RadioButton5"
        "
        "Label1
        "
        Me.Label1.Location = New System.Drawing.Point(40, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.TabIndex = 5
        Me.Label1.Text = "Label1"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.RadioButton5)
        Me.Controls.Add(Me.RadioButton4)
        Me.Controls.Add(Me.RadioButton3)
        Me.Controls.Add(Me.RadioButton2)
        Me.Controls.Add(Me.RadioButton1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub RadioButton1_CheckedChanged( _
            ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles RadioButton1.CheckedChanged, _
                    RadioButton2.CheckedChanged, _
                    RadioButton3.CheckedChanged, _
                    RadioButton4.CheckedChanged, _
                    RadioButton5.CheckedChanged
        Dim i As Integer = 0
        Dim found As Boolean = False
        While i < 5 And Not found
            If radioButtons(i).Checked Then
                found = True
                selectedOption = i + 1
                Label1.Text = CStr(selectedOption)
            End If
            i += 1
        End While
    End Sub
End Class

WithEvents and Event method

Public Class Tester
    Shared Dim WithEvents Click3 As New TripleClicker
    Public Shared Sub Main
        Click3.Click()
        Click3.Click()
        Click3.Click()
    End Sub
    Private Shared Sub Click3_TplClick(ByVal Text As String) Handles Click3.TplClick
        Console.WriteLine(Text)
    End Sub
    
End Class
Public Class TripleClicker
    Public Event TplClick(ByVal Text As String)
    Public Sub Click()
        Static intCount As Integer = 0
        intCount += 1
        If intCount >= 3 Then
            intCount = 0
            RaiseEvent TplClick("The button was triple clicked.")
        End If
    End Sub
End Class
The button was triple clicked.