Visual Basic 中的繼承事件處理常式疑難排解Troubleshooting Inherited Event Handlers in Visual Basic

本主題列出繼承元件中的事件處理常式所引發的常見問題。This topic lists common issues that arise with event handlers in inherited components.


事件處理常式中的程式碼會針對每個呼叫執行兩次Code in Event Handler Executes Twice for Every Call

  • 繼承的事件處理常式不能包含Handles子句。An inherited event handler must not include a Handles clause. 基類中的方法已經與事件相關聯,並會據此引發。The method in the base class is already associated with the event and will fire accordingly. 請從繼承的方法中移除 Handles 子句。Remove the Handles clause from the inherited method.

    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 語句,或任何處理相同事件的其他方法。If the inherited method does not have a Handles keyword, verify that your code does not contain an extra AddHandler Statement or any additional methods that handle the same event.

請參閱See also