Visual Basic - 复选框状态

时间:2015-09-29 11:06:49

标签: vb.net checkbox

好的,所以我想要做的是检查一个Checkbox并在这个例子中添加'将被检查>虽然我会检查如何弹出一个说法"你不能一次选择两个以上的值"或某些东西,所以他们无法检查两个盒子并使程序崩溃。这是我的代码:

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    Dim Addition As String
    Dim Subtraction As String
    Dim Multiplication As String

    Multiplication = CheckBox3.CheckState
    Subtraction = CheckBox2.CheckState
    Addition = CheckBox1.CheckState

    If Addition = CheckState.Checked Then
        'label icon of the current calculation sign 
        neum.Text = "+"
        'label icon of the current calculation sign
        Me.CheckBox2.CheckState = CheckState.Unchecked
        Me.CheckBox3.CheckState = CheckState.Unchecked
    End If

    If Addition = CheckState.Unchecked Then
        neum.Text = " "
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

如果您将AdditionSubtractionMultiplication声明为布尔而不是字符串,则您编写的代码应该有效

Dim Addition, Subtraction, Multiplication As Boolean

或者,由于您只引用其中一个变量,因此您可以完全摆脱它们。 (使用您正在采用的方法,您需要CheckBox2.CheckedChangedCheckBox2.CheckedChanged的类似处理程序,但没有理由让每个处理程序检查 all CheckBoxes;只有< em> one 它正在处理。

If CheckState.Checked Then
    neum.Text = "+"
    Me.CheckBox2.CheckState = CheckState.Unchecked
    Me.CheckBox3.CheckState = CheckState.Unchecked
Else
    neum.Text = " "
End If

如上所述,单选按钮是向用户提供他们可能只选择一个选项的选项列表的首选控件。以下是该方法的代码建议:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'You don't have to do this with code
    'You can manually set the "Tag" property in the designer instead
    RadioButton1.Tag = "+"
    RadioButton2.Tag = "-"
    RadioButton3.Tag = "*"
End Sub

Private Sub RadioButtonChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged
    neum.Text = Sender.Tag
End Sub