Share via


Visual Basic 中的繼承事件處理常式疑難排解

本主題列出繼承元件中的事件處理常式所引發的常見問題。

程序

事件處理常式中的程式碼會針對每個呼叫執行兩次

  • 繼承的事件處理常式不得包含 Handles 子句。 基底類別中的方法已經與事件產生關聯,因此會隨之引發。 從繼承的方法中移除 Handles 子句。

    ' INCORRECT
    Protected Overrides Sub Button1_Click(
        ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles Button1.Click
    
        ' The Handles clause will cause all code
        ' in this block to be executed twice.
    End Sub
    
  • 如果繼承的方法沒有 Handles 關鍵字,請確認您的程式碼不包含額外的 AddHandler 陳述式,或處理相同事件的任何其他方法。

另請參閱