0

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
Adam Beck
  • 275
  • 2
  • 14
  • Do you want to disable the button while the program is working on a previous request? like.. The user Clicks the button -> Button is Disabled-> Program performs the task-> button is enabled. – Nikhil Bhandari Jun 24 '11 at 15:25
  • No. Basically I already created the code for the Click event behind my control. When I put it on the form I can double click my button and a new click subroutine is automatically created (well, the skeleton). This would allow users to add their own Click event code. I want that disabled. Does that help? – Adam Beck Jun 24 '11 at 15:27
  • Ok.. I get it. Try @Bueller 's solution :) – Nikhil Bhandari Jun 24 '11 at 15:33

2 Answers2

2

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

Community
  • 1
  • 1
Bueller
  • 2,336
  • 17
  • 11
  • I'm sure this is the correct answer but I am quite unfamiliar with C#. I've looked at all 3 articles and can't make sense of much. Pathetically, I even used a converter which didn't help much. I guess I'm farther behind in .NET than I thought because I have only the slightest idea of what is going on with the code. – Adam Beck Jun 24 '11 at 15:50
2

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
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • This does work. I guess I was just hoping that I could override the Button Click event and make it hidden so that the user doesn't even have the option to put any of his/her code inside the Click Event (i.e. when using the Class Name:Method Name drop downs at the top, have the IDE remove the Event). Thank you though! – Adam Beck Jun 24 '11 at 16:41