Question Multiple click events for a button

BrianO

New member
Joined
Jun 8, 2011
Messages
2
Location
Near Pittsburgh,PA
Programming Experience
Beginner
Hi all,

Is it possible to have multiple click events for a button based on the button text? In other words, if I change the button text can i create a new click event based on that text but use the same button?

for example:

button1.text starts as "steel"
button2.text starts as "brass"

If I click button1 then the text would change to:

button1.text="Stainless"
button2.text="Cold rolled"

If I click button1 again a string would then be added to a textbox and the process would start over again. can I have a different click event or would I need to put an If/Then or Select/Case in the sub to handle this? I have 10 buttons I would need to do this with and each needing at least 3 different choices for each depending on the previous button press.
 
Two Suggestions..

If I click button1 again a string would then be added to a textbox and the process would start over again. can I have a different click event or would I need to put an If/Then or Select/Case in the sub to handle this? I have 10 buttons I would need to do this with and each needing at least 3 different choices for each depending on the previous button press.

Yes you could write one method to handle as many buttons as you need. This one method would need an if/select statement like you said. You can use the AddHandler, like

AddHandler Me.Click, AddressOf changeText


Then you can use the sender object from the click event to modify that buttons text.

Another approach i suggest is to write a custom class for your button or basically create the component (build the project and it should show up in your toolbox in VS.NET). If this is what your trying to achieve, writing the class might be a better idea than the if/select statements. Depends on your situation.. You can write a class that inherits button and adds a handler that raises the method to change the text. you can also add the items you want for that specific text box and have it change when its clicked. This way if you had ten buttons, like the code shown below, you can just create them like:

Dim btnSpecial As New myButton({"Word", "Sentence", "Ended"})
Dim btnSpecial2 As New myButton({"Wo", "tence", "ded"})
''...and so forth
''..or even do it in a loop if your situation permits to avoid the code redundancy.


then just add those new controls to your form or something


heres the examples
    'add this to the relevant buttons using addhandler, or using the Handles in this method sig
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim thisButton As Button = DirectCast(sender, Button)

        'write some if statement to do what you want.. something like...

        If thisButton.Text.Contains("ok") Then
            thisButton.Text &= "oooooh"
        Else
            thisButton.Text &= "ok"
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim btnSpecial As New myButton({"Word", "Sentence", "Ended"})
        btnSpecial.SetBounds(0, 0, 70, 20)

        Me.Controls.Add(btnSpecial)
    End Sub

    Class myButton
        Inherits Button

        Property textIndex As Integer = 0
        Private possibleText As New List(Of String)

        Sub New(ByVal ParamArray possibleText() As String)
            Me.possibleText.AddRange(possibleText)
            Me.Text = possibleText(0)

            AddHandler Me.Click, AddressOf changeText
        End Sub

        Sub changeText(ByVal sender As Object, ByVal e As EventArgs)
            If textIndex < possibleText.Count - 1 Then
                textIndex += 1
            Else
                textIndex = 0
            End If

            Me.Text = possibleText.Item(textIndex)
        End Sub
    End Class
 
Back
Top