2011-08-29 33 views
1

最近,我正在研究一個自定義按鈕,其中必須捕獲該按鈕的事件,並且根據特定條件,我會阻止事件或將它傳遞給包含我的自定義按鈕的表單。取消派生類中的按鈕點擊事件

這裏是代碼的原型我工作:

Public Class MyCustomButton 
    Inherits Windows.Forms.Button 

    Private Sub Me_Clicked(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Click 
     Dim i As Integer = MsgBox("Are you sure you want to perform the operation?", MsgBoxStyle.YesNoCancel, "MyApp") 
     If Not i = 6 Then 
      'Cancel the event 
     Else 
      'Continue with the event 
     End If 
    End Sub 
End Class 

現在,我沒有任何想法如何阻止該事件並不會讓它通過,如果用戶選擇採用在「否」給出的例子。 有什麼建議嗎?

回答

2

你需要重寫OnClick事件:

Public Class MyCustomButton 
    Inherits Button 

    Protected Overrides Sub OnClick(ByVal e As System.EventArgs) 
    Dim i As Integer = MsgBox("Are you sure you want to perform the operation?", MsgBoxStyle.YesNoCancel, "MyApp") 
    If Not i = 6 Then 
     'Cancel the event 
    Else 
     MyBase.OnClick(e) 
    End If 
    End Sub 

End Class 
+0

感謝LarsTech,精湛。 –