VB.net Multiple Buttons using one function

1.5k Views Asked by At

I dynamically rename buttons and I need to perform a function based on the button text. I have working code (5 sections for each button). Is there was good way to just use a single function for all 5 buttons using DirectCast or CType or really any other function so I don't have to have multiple functions that are doing the same thing?

My code example for two of the 5 buttons:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If Button1.Text = "GO" Then
      MsgBox("GO")
    ElseIf Button1.Text = "STOP" Then
      MsgBox("STOP")
    End If
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    If Button2.Text = "GO" Then
      MsgBox("GO")
    ElseIf Button2.Text = "STOP" Then
      MsgBox("STOP")
    End If
End Sub

Thanks in advance!

1

There are 1 best solutions below

0
ericpap On

Instead of direct reference Buton1 you can try this (Not sure about VB.NET sintax):

Dim myButton as Button
myButton=(button)sender
If myButton.Text = "GO" Then...