我动态重命名按钮,并且需要根据按钮文本执行一个功能。我有工作代码(每个按钮有5个部分)。有没有一个好的方法可以使用DirectCast或CType或任何其他函数为所有5个按钮使用一个函数,这样我就不必有多个函数在做同样的事情了?
我的5个按钮中的两个的代码示例:
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
提前感谢!
您可以尝试以下操作(不确定VB.NET sintax):
Dim myButton as Button
myButton=(button)sender
If myButton.Text = "GO" Then...