如何让代码在按钮上每次点击都不要将单词写入文本框?
当同时单击两个复选框时,必须按添加顺序写入文本,但是当我再次单击该按钮时,文本不应加倍或相乘。
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
答案 0 :(得分:1)
对每个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
这是有效的,正如您所看到的,我没有更改仅在我建议的内容中添加的代码。
答案 1 :(得分:0)
只需在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
答案 2 :(得分:0)
非常感谢您对我的问题的关注。在我检查你的解决方案后,我去睡觉了。我发布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
结束班