I have a Class that inherits the Button object. I have a simple function and an event handler for Click. When I put this on another Window开发者_如何学编程 Form - works perfect! However, I now want to be able to disable the user from selecting the 'Click' event when it is being used. Hopefully this makes sense.
As an attempt to clarify: I have a cusom button but I don't want the user to be able to program the Click event when putting it on a form.
I'm getting tired of putting this but... I'm obviously new to .NET
EDIT: Here is the code
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
I "think" I get what you are trying to do. Try overriding the OnClick event and just comment out the MyBase.OnClick(e) to prevent it from passing to the client:
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
And then from your form:
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
If I understand correctly, you have implemented your own custom button control derived from the Button class. As part of your custom button, you implemented your own click event handler that you do not want implementers of you control to be able to override.
Without seeing code, a suggestion would be to mark your class as "sealed". This would not necessarily prevent someone from registering a new click event handler with your button, but we would need to see some code and perhaps a better description of what you are trying to accomplish to really help you properly.
EDIT:
This may help.
C# pattern to prevent an event handler hooked twice
精彩评论