簡體   English   中英

VB.net使用一個功能的多個按鈕

[英]VB.net Multiple Buttons using one function

我動態重命名按鈕,並且需要基於按鈕文本執行功能。 我有工作代碼(每個按鈕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

提前致謝!

您可以嘗試以下方法,而不是直接參考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