简体   繁体   中英

VB.net Multiple Buttons using one function

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!

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM