简体   繁体   English

VB.net使用一个功能的多个按钮

[英]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). 我有工作代码(每个按钮5个部分)。 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? 有没有一种好方法,可以使用DirectCast或CType或所有其他功能仅对所有5个按钮使用一个功能,因此我不必具有多个在做同一件事的功能?

My code example for two of the 5 buttons: 我的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

Thanks in advance! 提前致谢!

Instead of direct reference Buton1 you can try this (Not sure about VB.NET sintax): 您可以尝试以下方法,而不是直接参考Buton1(不确定VB.NET sintax):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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