2016-04-26 75 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