如何计算在visual basic中检查的复选框的数量?

时间:2016-04-26 11:09:44

标签: vb.net

Private Sub Btn_Cast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Cast.Click
    Dim Counter As Integer = 0
    If ChkBox_Barton.CheckState = 1 And ChkBox_Martin.CheckState = 1 And ChkBox_Richards.CheckState = 1 Then
        MsgBox("Don't vote for more than 2")
    End If
    Dim Count_Barton As Integer
    Dim Count_Martin As Integer
    Dim Count_Richards As Integer

    If ChkBox_Barton.Checked Then Count_Barton += 1

    If ChkBox_Martin.Checked = 1 Then Count_Martin += 1

    If ChkBox_Richards.CheckState = 1 Then Count_Richards += 1

End Sub

问题是,我每次都试图计算它,然后让它重置并再次计数。

实施例。我选择巴顿一次,点击投票,然后我应该能够选择新的人并点击投票,它应该继续计算。

我该怎么办?

然后我需要显示我的结果。我应该只保留文本或整数文件中的数字然后以这种方式显示它吗?

2 个答案:

答案 0 :(得分:0)

    Dim Count_Barton As Integer = 0
    Dim Count_Martin As Integer = 0
    Dim Count_Richards As Integer = 0

Private Sub Btn_Cast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Cast.Click
    Dim Counter As Integer = 0 'NOT SURE WHAT THIS IS DOING... NOT BEING USED
    If ChkBox_Barton.CheckState = 1 And ChkBox_Martin.CheckState = 1 And ChkBox_Richards.CheckState = 1 Then
        MsgBox("Don't vote for more than 2")
    Else
        If ChkBox_Barton.Checked Then Count_Barton += 1

        If ChkBox_Martin.Checked = 1 Then Count_Martin += 1

        If ChkBox_Richards.CheckState = 1 Then Count_Richards += 1

    End If     

End Sub

答案 1 :(得分:0)

我自己快速设置你的应用程序。

以下代码适用于此GUI:

enter image description here

代码:

Public Class VoteCounter

Dim intCountBarton As Integer
Dim intCountMartin As Integer
Dim intCountRichards As Integer

Private Sub ButtonVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonVote.Click

    If CheckBoxBarton.CheckState = 1 And CheckBoxMartin.CheckState = 1 And CheckBoxRichards.CheckState = 1 Then
        MsgBox("Don't vote for more than 2")
        CheckBoxBarton.Checked = False
        CheckBoxMartin.Checked = False
        CheckBoxRichards.Checked = False
    End If

    If CheckBoxBarton.Checked Then
        intCountBarton += 1
    End If

    If CheckBoxMartin.Checked Then
        intCountMartin = intCountMartin + 1
    End If

    If CheckBoxRichards.Checked Then
        intCountRichards = intCountRichards + 1
    End If

    CheckBoxBarton.Checked = False
    CheckBoxMartin.Checked = False
    CheckBoxRichards.Checked = False

End Sub

Private Sub ButtonResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonResult.Click

    MsgBox("Barton: " & intCountBarton & vbNewLine & "Martin: " & intCountMartin & vbNewLine & "Richards: " & intCountRichards)

End Sub

Private Sub ButtonReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReset.Click

    CheckBoxBarton.Checked = False
    CheckBoxMartin.Checked = False
    CheckBoxRichards.Checked = False

    intCountBarton = 0
    intCountMartin = 0
    intCountRichards = 0

End Sub

End Class