RaiseEvent 语句RaiseEvent Statement

触发在类、窗体或文档中的模块级别声明的事件。Triggers an event declared at module level within a class, form, or document.

语法Syntax

RaiseEvent eventname[( argumentlist )]  

部件Parts

eventname
必需。Required. 要触发的事件的名称。Name of the event to trigger.

argumentlist
可选。Optional. 以逗号分隔的变量、数组或表达式的列表。Comma-delimited list of variables, arrays, or expressions. argumentlist 参数必须括在括号中。The argumentlist argument must be enclosed by parentheses. 如果没有参数,则必须省略括号。If there are no arguments, the parentheses must be omitted.

备注Remarks

必需的 eventname 是在模块中声明的事件的名称。The required eventname is the name of an event declared within the module. 它遵循 Visual Basic 变量命名约定。It follows Visual Basic variable naming conventions.

如果未在引发事件的模块内声明事件,则会发生错误。If the event has not been declared within the module in which it is raised, an error occurs. 下面的代码段演示了事件声明和引发事件的过程。The following code fragment illustrates an event declaration and a procedure in which the event is raised.

' Declare an event at module level.
Event LogonCompleted(ByVal UserName As String)

Sub Logon(ByVal UserName As String)
    ' Raise the event.
    RaiseEvent LogonCompleted(UserName)
End Sub

不能使用 RaiseEvent 引发未在模块中显式声明的事件。You cannot use RaiseEvent to raise events that are not explicitly declared in the module. 例如,所有窗体都从 System.Windows.Forms.Form继承 Click 事件,不能使用派生窗体中的 RaiseEvent 来引发该事件。For example, all forms inherit a Click event from System.Windows.Forms.Form, it cannot be raised using RaiseEvent in a derived form. 如果在窗体模块中声明 Click 事件,则会隐藏窗体自身的 Click 事件。If you declare a Click event in the form module, it shadows the form's own Click event. 你仍可以通过调用 OnClick 方法来调用窗体的 Click 事件。You can still invoke the form's Click event by calling the OnClick method.

默认情况下,在 Visual Basic 中定义的事件将按照建立连接的顺序引发其事件处理程序。By default, an event defined in Visual Basic raises its event handlers in the order that the connections are established. 由于事件可以有 ByRef 参数,因此,后期连接的进程可能会收到先前的事件处理程序已更改的参数。Because events can have ByRef parameters, a process that connects late may receive parameters that have been changed by an earlier event handler. 在事件处理程序执行后,控制将返回到引发事件的子例程。After the event handlers execute, control is returned to the subroutine that raised the event.

备注

非共享事件不应在声明它们的类的构造函数中引发。Non-shared events should not be raised within the constructor of the class in which they are declared. 尽管此类事件不会导致运行时错误,但是它们可能无法被关联的事件处理程序捕获。Although such events do not cause run-time errors, they may fail to be caught by associated event handlers. 如果需要从构造函数引发事件,请使用 Shared 修饰符创建共享事件。Use the Shared modifier to create a shared event if you need to raise an event from a constructor.

备注

可以通过定义自定义事件来更改事件的默认行为。You can change the default behavior of events by defining a custom event. 对于自定义事件,RaiseEvent 语句调用事件的 RaiseEvent 访问器。For custom events, the RaiseEvent statement invokes the event's RaiseEvent accessor. 有关自定义事件的详细信息,请参阅Event 语句For more information on custom events, see Event Statement.

示例Example

下面的示例使用事件从 10 秒到 0 秒进行倒计时。The following example uses events to count down seconds from 10 to 0. 此代码演示了一些与事件相关的方法、属性和语句,包括 RaiseEvent 语句。The code illustrates several 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.

该示例还使用一个窗体 (Form1),其中包含一个按钮 (Button1) 和一个文本框 (TextBox1)。The example also uses a form (Form1) with a button (Button1) and a text box (TextBox1). 单击该按钮时,第一个文本框显示从 10 秒到 0 秒的倒计时。When you click the button, the first text box displays a countdown from 10 to 0 seconds. 经过了全部时间(10 秒)之后,第一个文本框会显示“Done”。When the full time (10 seconds) has elapsed, the first text box displays "Done".

Form1 的代码指定窗体的初始和最终状态。The code for Form1 specifies the initial and terminal states of the form. 它还包含引发事件时执行的代码。It also contains the code executed when events are raised.

若要使用此示例,请打开一个新的 "Windows 应用程序" 项目,添加名为 "Button1" 的按钮,并将名为 TextBox1 的文本框添加到名为 Form1的主窗体To use this example, open a new Windows Application project, add a button named Button1 and a text box named TextBox1 to the main form, named Form1. 然后,右键单击该窗体,然后单击 "查看代码" 以打开代码编辑器。Then right-click the form and click View Code to open the Code Editor.

WithEvents 变量添加到 Form1 类的声明部分。Add a WithEvents variable to the declarations section of the Form1 class.

Private WithEvents mText As TimerState

示例Example

将下面的代码添加到 Form1 的代码:Add the following code to the code for Form1. 替换任何可能存在的重复过程,如 Form_LoadButton_ClickReplace any duplicate procedures that may exist, such as Form_Load, or Button_Click.

Private Sub Form1_Load() Handles MyBase.Load
    Button1.Text = "Start"
    mText = New TimerState
End Sub
Private Sub Button1_Click() Handles Button1.Click
    mText.StartCountdown(10.0, 0.1)
End Sub

Private Sub mText_ChangeText() Handles mText.Finished
    TextBox1.Text = "Done"
End Sub

Private Sub mText_UpdateTime(ByVal Countdown As Double
  ) Handles mText.UpdateTime

    TextBox1.Text = Format(Countdown, "##0.0")
    ' Use DoEvents to allow the display to refresh.
    My.Application.DoEvents()
End Sub

Class TimerState
    Public Event UpdateTime(ByVal Countdown As Double)
    Public Event Finished()
    Public Sub StartCountdown(ByVal Duration As Double, 
                              ByVal Increment As Double)
        Dim Start As Double = DateAndTime.Timer
        Dim ElapsedTime As Double = 0

        Dim SoFar As Double = 0
        Do While ElapsedTime < Duration
            If ElapsedTime > SoFar + Increment Then
                SoFar += Increment
                RaiseEvent UpdateTime(Duration - SoFar)
            End If
            ElapsedTime = DateAndTime.Timer - Start
        Loop
        RaiseEvent Finished()
    End Sub
End Class

按 F5 运行前面的示例,并单击标签为 "启动" 的按钮。Press F5 to run the preceding example, and click the button labeled Start. 第一个文本框中开始倒计时秒数。The first text box starts to count down the seconds. 经过了全部时间(10 秒)之后,第一个文本框会显示“Done”。When the full time (10 seconds) has elapsed, the first text box displays "Done".

备注

My.Application.DoEvents 方法不以与窗体相同的方式处理事件。The My.Application.DoEvents method does not process events in exactly the same way as the form does. 若要允许窗体直接处理事件,可以使用多线程处理。To allow the form to handle the events directly, you can use multithreading. 有关详细信息,请参阅托管线程处理For more information, see Managed Threading.

另请参阅See also