Optional. Specifies that the Event is visible throughout the project. Events types are Public by default. Note that events can only be raised in the module in which they are declared.
procedurename
Required. Name of the event; follows standard variable naming conventions.
The arglist argument has the following syntax and parts:
After the event has been declared, use the RaiseEvent statement to fire the event. A syntax error occurs if an Event declaration appears in a standard module. An event can't be declared to return a value. A typical event might be declared and raised as shown in the following fragments.
VB
' Declare an event at module level of a class module Event LogonCompleted (UserName asString)
SubRaiseEvent LogonCompleted("AntoineJan")
EndSub
Note
You can declare event arguments just as you do arguments of procedures, with the following exceptions: events cannot have named arguments, Optional arguments, or ParamArray arguments. Events don't have return values.
Example
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 Event statement.
The class that raises an event is the event source, and the classes that implement the event are the sinks. An event source can have multiple sinks for the events it generates. When the class raises the event, that event is fired on every class that has elected to sink 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 specifies the initial and terminal states of the form. It also contains the code executed when events are raised.
VB
OptionExplicitPrivateWithEvents mText As TimerState
PrivateSub Command1_Click()
Text1.Text = "From Now"
Text1.Refresh
Text2.Text = "0"
Text2.Refresh
Call mText.TimerTask(9.84)
EndSubPrivateSub Form_Load()
Command1.Caption = "Click to Start Timer"
Text1.Text = ""
Text2.Text = ""
Label1.Caption = "The fastest 100 meter run took this long:"Set mText = New TimerState
EndSubPrivateSub mText_ChangeText()
Text1.Text = "Until Now"
Text2.Text = "9.84"EndSubPrivateSub mText_UpdateTime(ByVal dblJump AsDouble)
Text2.Text = Str(Format(dblJump, "0"))
DoEvents
EndSub
The remaining code is in a class module named TimerState. The Event statements declare the procedures initiated when events are raised.
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
Do you want to know how to add your business logic to existing functionality in Microsoft Dynamics 365 Business Central? In this module, you'll learn how to add code and work with events.