2016-04-26 52 views
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 
    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 

问题是,我试图每次计算它,然后让它重置并重新计数。如何计算在visual basic中检查的复选框数量?

例子。我选择巴顿一次,点击投票,然后我应该能够选择一个新的和点击投票,它应该继续计数。

我该怎么办?

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

+0

首先你的变量Count_Barton的声明,Count_Martin,Count_Richards一定不能Btn_Cast_Click事件中,因为你对它们进行初始化每次点击投票按钮 –

+0

您是否想要使用单选按钮,以便您一次投一票,或者您可以同时投多个人? – ja72

回答

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 
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 
+0

我在弹出的问题上采取了您的观点,并修改了代码以解决此问题...至于其他评论...在VB中...如果语句可以在没有和可以使用的情况下使用,线.... – Mych