【问题标题】:Make button click work for once使按钮单击工作一次
【发布时间】:2012-12-11 03:06:27
【问题描述】:

如何告诉代码不要在每次点击按钮时将单词写入文本框?

当同时单击两个复选框时,必须按添加顺序写入文本,但是当我再次单击按钮时,文本不应加倍或相乘。

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If CheckBox1.Checked = True Then
            TextBox1.Text += ("hello ")
        End If
        If CheckBox2.Checked = True Then
            TextBox1.Text += ("please help")
        End If
    End Sub
End Class

【问题讨论】:

    标签: vb.net button checkbox textbox click


    【解决方案1】:

    非常感谢您对我的问题的关注。在我检查了你的解决方案后,我上床睡觉了。我刚刚发布了stg,我正要睡觉并再次打开电脑并解决了我用这个东西解决的愚蠢问题..这对我来说非常适合:)

        Public Class Form1
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Textbox1.Text= ("")
        If CheckBox1.Checked = True Then
            TextBox1.Text += ("hello ")
        End If
        If CheckBox2.Checked = True Then
            TextBox1.Text += ("please help")
        End If
    End Sub
    

    结束类

    【讨论】:

      【解决方案2】:

      为每个 if 语句(即每个复选框)使用布尔变量。最初将它们设置为 false 并将您的代码更改为如下所示

      If CheckBox1.Checked = True And CheckBox1Bool = False Then
          TextBox1.Text += ("hello ")
          CheckBox1Bool = True
      End If
      If CheckBox2.Checked = True And CheckBox2Bool = False Then
          TextBox1.Text += ("please help")
          CheckBox2Bool = True
      End If
      

      编辑:

      Public Class Form1
          Dim Bool1 As Boolean
          Dim Bool2 As Boolean
      
          Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
              If CheckBox1.Checked = True And Not Bool1 Then
                  TextBox1.Text += ("hello ")
                  Bool1 = True
              End If
              If CheckBox2.Checked = True And Not Bool2 Then
                  TextBox1.Text += ("please help")
                  Bool2 = True
              End If
          End Sub
      End Class
      

      这行得通,正如您所见,我没有更改代码,只是添加了我向您建议的内容。

      【讨论】:

      • Dim CheckBox1Bool As Boolean VB中布尔值的默认值为false,所以你甚至不需要一开始就设置值
      • 是的,并将其声明为来自成员,而不是作为局部变量。
      • 我更喜欢写If CheckBox1.Checked And Not CheckBox1Bool Then
      • 感谢您的关心,但这不是我要的。这样,如果我再次单击按钮,则编码为复选框的文本会一次又一次地写入文本框。如果我点击按钮三下,单词被写了三遍..即使我点击按钮十或二十次,我也只想写一次……
      • 那么布尔变量的范围不正确。范围必须大于您所拥有的范围
      【解决方案3】:

      只需在 Button Click 事件中重置 Checkbox.Checked 事件。这样它就不会再次发送文本,直到您重新选择它。

      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
          If CheckBox1.Checked = True Then
              CheckBox1.Checked = False
              TextBox1.Text += ("hello ")
          End If
          If CheckBox2.Checked = True Then
              CheckBox2.Checked = False
              TextBox1.Text += ("please help")
          End If
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多