Declares a user-defined event.
[ <attrlist> ] [ Public | Private | Protected | Friend | Protected Friend ] _ [ Shadows ] Event eventname[(arglist)] _ [ Implements interfacename.interfaceeventname ]
Each attribute in the attrlist part has the following syntax and parts:
attrname [({ attrargs | attrinit })]
The arglist argument has the following syntax and parts:
[ <attrlist> ] [ ByVal | ByRef ] varname[ ( ) ] [ As type ]
Once the event has been declared, use the RaiseEvent statement to raise the event. A typical event might be declared and raised as shown in the following fragments:
Public Class EventSource ' Declare an event. Public Event LogonCompleted(ByVal UserName As String) Sub CauseEvent() ' Raise an event on successful logon. RaiseEvent LogonCompleted("AustinSteele") End Sub End Class
Note You can declare event arguments just as you do arguments of procedures, with the following exceptions: events cannot have named arguments, or Optional arguments. Events do not have return values.
The following example uses events to count off seconds during a demonstration of the fastest 100 meter race. The code illustrates all of the event-related methods, properties, and statements, including the RaiseEvent statement.
The class that raises an event is the event source, and the methods that process the event are the event handlers. An event source can have multiple handlers for the events it generates. When the class raises the event, that event is raised on every class that has elected to handle events for that instance of the object.
The example also uses a form (Form1
) with a button (Command1
), a label (Label1
), and two text boxes (Text1
and Text2
). When you click the button, the first text box displays "From Now" and the second starts to count seconds. When the full time (9.84 seconds) has elapsed, the first text box displays "Until Now" and the second displays "9.84"
The code for Form1
specifies the initial and terminal states of the form. It also contains the code executed when events are raised.
To use this example, open a new Windows Forms project, add a button named Button1
, a label named Label1
and two text boxes, named TextBox1
and TextBox2
, to the main form, named form1
. Then right click the form and click View Code to open the code editor.
To simplify access to the Timer property, add an Imports statement as the first line of code above the Class Form1
statement.
Imports Microsoft.VisualBasic.DateAndTime
Add a WithEvents variable to the declarations section of the Form1
class.
Private WithEvents mText As TimerState
Add the following code to the code for Form1
. Replace any duplicate procedures that may exist, such as Form_Load
, or Button_Click
.
Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Button1.Text = "Click to Start Timer" TextBox1.Text = "" TextBox2.Text = "" Label1.Text = "The fastest 100 meters ever run took this long:" mText = New TimerState() End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click TextBox1.Text = "From Now" TextBox1.Refresh() TextBox2.Text = "0" TextBox2.Refresh() mText.TimerTask(9.84) End Sub Private Sub mText_ChangeText() Handles mText.ChangeText TextBox1.Text = "Until Now" TextBox2.Text = "9.84" End Sub Private Sub mText_UpdateTime(ByVal Jump As Double) _ Handles mText.UpdateTime TextBox2.Text = Format(Jump, "##0.00") Application.DoEvents() End Sub Class TimerState Public Event UpdateTime(ByVal Jump As Double) Public Event ChangeText() Public Sub TimerTask(ByVal Duration As Double) Dim Start As Double Dim Second As Double Dim SoFar As Double Start = Timer SoFar = Start Do While Timer < Start + Duration If Timer - SoFar >= 0.1 Then SoFar = SoFar + 0.1 RaiseEvent UpdateTime(Timer - Start) End If Loop RaiseEvent ChangeText() End Sub End Class
Press F5 To run this example, and click the button labeled Click to start timer. The first text box displays "From Now" and the second starts to count seconds. When the full time (9.84 seconds) has elapsed, the first text box displays "Until Now" and the second displays "9.84".
RaiseEvent Statement | Implements Statement |