Handles 子句 (Visual Basic)Handles Clause (Visual Basic)

宣告程序會處理指定的事件。Declares that a procedure handles a specified event.

語法Syntax

proceduredeclaration Handles eventlist  

組件Parts

proceduredeclaration
將會處理事件之程序的 Sub 程序宣告。The Sub procedure declaration for the procedure that will handle the event.

eventlist
要處理之 proceduredeclaration 的事件清單,以逗號分隔。List of the events for proceduredeclaration to handle, separated by commas. 事件必須由目前類別的基底類別引發,或由使用 WithEvents 關鍵字宣告的物件引發。The events must be raised by either the base class for the current class, or by an object declared using the WithEvents keyword.

備註Remarks

在程序宣告結尾使用 Handles 關鍵字,讓它處理由使用 WithEvents 關鍵字宣告之物件變數引發的事件。Use the Handles keyword at the end of a procedure declaration to cause it to handle events raised by an object variable declared using the WithEvents keyword. Handles 關鍵字也可以在衍生的類別中使用,以處理基底類別中的事件。The Handles keyword can also be used in a derived class to handle events from a base class.

Handles 關鍵字和 AddHandler 陳述式都可以讓您指定由特定程序處理特定事件,但兩者存有差異。The Handles keyword and the AddHandler statement both allow you to specify that particular procedures handle particular events, but there are differences. 當定義程序以指定它處理特定事件時,使用 Handles 關鍵字。Use the Handles keyword when defining a procedure to specify that it handles a particular event. AddHandler 陳述式會在執行階段將程序連接到事件。The AddHandler statement connects procedures to events at run time. 如需詳細資訊,請參閱AddHandler 語句For more information, see AddHandler Statement.

對於自訂事件,當應用程式將程式新增為事件處理常式時,應用程式會叫用事件的 AddHandler 存取子。For custom events, the application invokes the event's AddHandler accessor when it adds the procedure as an event handler. 如需自訂事件的詳細資訊,請參閱Event 語句For more information on custom events, see Event Statement.

範例Example

Public Class ContainerClass
    ' Module or class level declaration.
    WithEvents Obj As New Class1

    Public Class Class1
        ' Declare an event.
        Public Event Ev_Event()
        Sub CauseSomeEvent()
            ' Raise an event.
            RaiseEvent Ev_Event()
        End Sub
    End Class

    Sub EventHandler() Handles Obj.Ev_Event
        ' Handle the event.
        MsgBox("EventHandler caught event.")
    End Sub

    ' Call the TestEvents procedure from an instance of the ContainerClass 
    ' class to test the Ev_Event event and the event handler.
    Public Sub TestEvents()
        Obj.CauseSomeEvent()
    End Sub
End Class

下列範例示範衍生的類別如何使用 Handles 陳述式來處理基底類別中的事件。The following example demonstrates how a derived class can use the Handles statement to handle an event from a base class.

Public Class BaseClass
    ' Declare an event.
    Event Ev1()
End Class
Class DerivedClass
    Inherits BaseClass
    Sub TestEvents() Handles MyBase.Ev1
        ' Add code to handle this event.
    End Sub
End Class

範例Example

下列範例包含WPF 應用程式專案的兩個按鈕事件處理常式。The following example contains two button event handlers for a WPF Application project.

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
    MessageBox.Show(sender.Name & " clicked")
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
    MessageBox.Show(sender.Name & " clicked")
End Sub

範例Example

下列範例等同於先前的範例。The following example is equivalent to the previous example. Handles 子句中的 eventlist 包含兩個按鈕的事件。The eventlist in the Handles clause contains the events for both buttons.

Private Sub Button_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click, Button2.Click
    MessageBox.Show(sender.Name & " clicked")
End Sub

請參閱See also