0

ボタンをクリックするたびにテキストボックスに単語を書き込まないようにコードに指示するにはどうすればよいですか?

両方のチェックボックスが両方ともクリックされた場合、テキストは加算順に書き込まれる必要がありますが、ボタンをもう一度クリックすると、テキストは 2 倍または乗算されません。

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
4

3 に答える 3

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

これは機能し、ご覧のとおり、提案したものに追加されたコードのみを変更していません。

于 2012-12-24T21:10:39.107 に答える
0

Just reset your Checkbox.Checked event in your Button Click event. That way it will not send the text again till you reselect it.

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
于 2012-12-24T21:46:39.847 に答える
0

私の問題についてご心配いただき、誠にありがとうございます。あなたの解決策を確認した後、私は寝ました。stg をリリースしただけで、寝るつもりだったので、PC をもう一度開いて、このばかげた問題を解決しました。これは私にとって完璧に機能します :)

    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

クラス終了

于 2012-12-25T11:31:22.853 に答える