【问题标题】:VB.net Multiple Buttons using one functionVB.net 多个按钮使用一个功能
【发布时间】:2016-03-20 09:56:30
【问题描述】:

我动态重命名按钮,我需要根据按钮文本执行功能。我有工作代码(每个按钮 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

提前致谢!

【问题讨论】:

  • Ctype 等适用于类型而不是方法。只需将它们连接到同一个处理程序
  • @varocarbas - 谢谢,这对我正在做的事情有用。
  • @varocarbas 已经包含在欺骗链接中。
  • 抱歉会稍作标记。谢谢大家!

标签: vb.net button ctype directcast


【解决方案1】:

你可以试试这个,而不是直接引用 Buton1(不确定 VB.NET sintax):

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2013-04-06
    • 2017-12-28
    • 2023-03-24
    • 2021-06-30
    • 1970-01-01
    相关资源
    最近更新 更多