편집

다음을 통해 공유


RaiseEvent statement

Fires an event declared at the module level within a class, form, or document.

Syntax

RaiseEventeventname [ ( argumentlist ) ]

The required eventname is the name of an event declared within the module and follows Basic variable naming conventions.

The RaiseEvent statement syntax has these parts:

Part Description
eventname Required. Name of the event to fire.
argumentlist Optional. Comma-delimited list of variables, arrays, or expressions. The argumentlist must be enclosed by parentheses. If there are no arguments, the parentheses must be omitted.

Remarks

If the event has not been declared within the module in which it is raised, an error occurs. The following fragment illustrates an event declaration and a procedure in which the event is raised.

' Declare an event at module level of a class module 
Event LogonCompleted (UserName as String) 
 
Sub 
 ' Raise the event. 
 RaiseEvent LogonCompleted ("AntoineJan") 
End Sub

If the event has no arguments, including empty parentheses in the RaiseEvent invocation of the event causes an error. You can't use RaiseEvent to fire events that are not explicitly declared in the module.

For example, if a form has a Click event, you can't fire its Click event by using RaiseEvent. If you declare a Click event in the form module, it shadows the form's own Click event. You can still invoke the form's Click event by using normal syntax for calling the event, but not by using the RaiseEvent statement.

Event firing is done in the order that the connections are established. Because events can have ByRef parameters, a process that connects late may receive parameters that have been changed by an earlier event handler.

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 RaiseEvent 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.58 seconds) has elapsed, the first text box displays Until Now and the second displays 9.58.

The code specifies the initial and terminal states of the form. It also contains the code executed when events are raised.

Option Explicit

Private WithEvents ts As TimerState
Private Const FinalTime As Double = 9.58

Private Sub UserForm_Initialize()
    Command1.Caption = "Click to start timer"
    Text1.Text = vbNullString
    Text2.Text = vbNullString
    Label1.Caption = "The fastest 100 meters ever run took this long:"
    Set ts = New TimerState
End Sub

Private Sub Command1_Click()
    Text1.Text = "From Now"
    Text2.Text = "0"
    ts.TimerTask FinalTime
End Sub

Private Sub ts_UpdateElapsedTime(ByVal elapsedTime As Double)
    Text2.Text = CStr(Format(elapsedTime, "0.00"))
End Sub

Private Sub ts_DisplayFinalTime()
    Text1.Text = "Until now"
    Text2.Text = CStr(FinalTime)
End Sub

The remaining code is in a class module named TimerState. Included among the commands in this module are the Raise Event statements.

Option Explicit

Public Event UpdateElapsedTime(ByVal elapsedTime As Double)
Public Event DisplayFinalTime()
Private Const delta As Double = 0.01

Public Sub TimerTask(ByVal duration As Double)
    Dim startTime As Double
    startTime = Timer
    Dim timeElapsedSoFar As Double
    timeElapsedSoFar = startTime
    
    Do While Timer < startTime + duration
        If Timer - timeElapsedSoFar >= delta Then
            timeElapsedSoFar = timeElapsedSoFar + delta
            RaiseEvent UpdateElapsedTime(Timer - startTime)
            DoEvents
        End If
    Loop
    
    RaiseEvent DisplayFinalTime
End Sub

See also

Support and feedback

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.