이 프로그램은 투표에 유효한 후보자를 수락하고 텍스트 상자에 입력 한 이름을 목록 상자에 추가합니다. 목록 상자에서 사용자는 선택한 후보를 두 번 클릭 할 수 있습니다. 집계 버튼을 클릭하면 후보자의 이름과 득표 수를 표시하는 목록 상자가 다른 목록 상자 옆에 나타납니다.왜 하나의 결과 만 표시합니까?
제 문제는 lstTallies가 마지막으로 투표 한 후보만을 표시한다는 것입니다. 다음은이 시도 내 코드
Public Class Form1
Dim maxVotes As Integer
Dim winner As String
Dim votes() As Integer
Dim index As Integer
Dim candidates As String
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
If Not isValidInput(txtNewCandidate.Text) Then
Exit Sub
End If
lstCandidates.Items.Add(txtNewCandidate.Text)
txtNewCandidate.Clear()
txtNewCandidate.Focus()
ReDim Preserve votes(index)
index += 1
End Sub
Private Function isValidInput(ByRef firstName As String) As Boolean
If IsNumeric(txtNewCandidate.Text) Or txtNewCandidate.Text = "" Then
MsgBox("Please input a valid candidate name.")
txtNewCandidate.Focus()
Return False
Else
Return True
End If
End Function
Private Sub btnTally_Click(sender As Object, e As EventArgs) Handles btnTally.Click
lstTallies.Visible = True
lblTally.Visible = True
lstTallies.Items.Add(lstCandidates.Text & " " & votes(lstCandidates.SelectedIndex))
End Sub
Private Sub lstCandidates_DoubleClick(sender As Object, e As EventArgs) Handles lstCandidates.DoubleClick
If lstCandidates.SelectedIndex = -1 Then
MsgBox("Select a candidate by double-clicking")
End If
votes(lstCandidates.SelectedIndex) += 1
MsgBox("Vote Tallied")
End Sub
End Class
문제는 'lstCandidates'를 통해 '반복'하지 않는 것입니다. 또한 조언을 통해 색인을 사용하는 것이 신뢰할 수없는 경우 후보에 _CandidateID_와 같은 식별자가 있어야합니다. –
나는 iterating throuhg lstCandidates 시도하고 거기에 투표의 양을 반복 마지막 클릭 한 후보를 반환했습니다. –
후보자와 그/그녀의 표의 색인은 동일합니까? –