我有一个继承Button对象的Class。我有一个简单的函数和Click的事件处理程序。当我把它放在另一个窗口形式 - 工作完美!但是,我现在希望能够禁止用户在使用时选择“Click”事件。希望这是有道理的。
试图澄清:我有一个cusom按钮,但我不希望用户在将Click事件放在表单上时对其进行编程。
我已经厌倦了这个但是......我显然是.NET的新手
编辑:这是代码
Public NotInheritable Class clsRandomGenerator
Inherits Button
Protected Const ALPHA_UPPERCASE As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Protected Const ALPHA_LOWERCASE As String = "abcdefghijklmnopqrstuvwxyz"
Protected Const NUMERIC As String = "0123456789"
Protected Const SPECIAL_CHAR As String = "!@#$%^&*"
Protected Const ALPHA_NUMERIC As String = ALPHA_UPPERCASE & ALPHA_LOWERCASE & NUMERIC & SPECIAL_CHAR
Public Shared Function GetRandomPassword() As String
Dim RandomData As String = String.Empty
Dim intPosition As Integer = 0
Dim intLength As Integer = 8
Dim data As Byte() = New Byte(intLength) {}
Dim charSetLength As Integer = ALPHA_NUMERIC.Length
Dim randomize As RandomNumberGenerator = RandomNumberGenerator.Create()
randomize.GetBytes(data)
For index As Integer = 0 To intLength - 1
intPosition = data(index)
intPosition = intPosition Mod charSetLength
RandomData = (RandomData + ALPHA_NUMERIC.Substring(intPosition, 1))
Next
Return RandomData
End Function
Private Sub clsRandomGenerator_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
MessageBox.Show(GetRandomPassword)
End Sub
答案 0 :(得分:2)
如果我理解正确,您已经实现了自己从Button类派生的自定义按钮控件。作为自定义按钮的一部分,您实现了自己的单击事件处理程序,您不希望控件的实现者能够覆盖。
如果没有看到代码,建议将您的课程标记为“密封”。这并不一定会阻止某人使用您的按钮注册新的单击事件处理程序,但我们需要查看一些代码,并且可能更好地描述您要完成的操作以真正帮助您。
编辑:
这可能有所帮助。
答案 1 :(得分:2)
我“想”我得到了你想做的事。尝试重写OnClick事件并注释掉MyBase.OnClick(e)以防止它传递给客户端:
Public Class ButtonEx
Inherits Button
Protected Overrides Sub OnClick(e As EventArgs)
'MyBase.OnClick(e) 'Eat it
MessageBox.Show("Clicks from inside!")
End Sub
End Class
然后从你的表格:
Private Sub ButtonEx1_Click(sender As Object, e As EventArgs) Handles ButtonEx1.Click
'This should not pop up:
MessageBox.Show("Does this click?")
End Sub