取消其派生类中的按钮单击事件

时间:2011-08-29 13:25:18

标签: vb.net

最近,我正在制作一个自定义按钮,我必须捕捉按钮事件,并根据特定条件,我将阻止事件或将其传递到包含我的自定义按钮的表单上

这是我正在处理的代码的原型:

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

现在,如果用户在给定示例中选择“否”,我不知道如何阻止事件并且不允许它通过。 有什么建议吗?

1 个答案:

答案 0 :(得分: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