0

This program is supposed to accept in valid candidates for voting, add the names typed in a text box to a list box. In the list box the user may double click on the candidate they choose. After the tally button is clicked a list box displaying the candidates' Names and votes will appear along side the other list box.

My problem is that the lstTallies only displays the last voted candidate. Below is my code

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
  • The problem is you are not `iterating` through `lstCandidates`. Also just an advice, using an index is not reliable in what you are doing, you must have some sort of identifier like _CandidateID_ for your candidates. – Aethan Sep 15 '16 at 08:14
  • I tried iterating throuhg lstCandidates and it returned the last clicked candidate repeated to the amount of votes there are. – Justin de Gois Sep 15 '16 at 08:19
  • The index of the Candidate and his/her vote is the same? – Aethan Sep 15 '16 at 08:21
  • I used a for loop then, and it displayed the last selected candidate's name and their total votes, repeated to the total amount of times the first list box was double clicked. – Justin de Gois Sep 15 '16 at 08:28
  • Yes, I know about the problem. What I am asking is regarding on what you are trying to do. Anyway, kindly check the answer I posted and check it if it works. – Aethan Sep 15 '16 at 08:29

1 Answers1

0

Try this:

Assuming the index of the Candidate and his/her Vote are the same:

  Private Sub btnTally_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnTally.Click
    lstTallies.Visible = True
    lblTally.Visible = True
    For i = 0 To lstCandidates.Items.Count - 1
        lstTallies.Items.Add(lstCandidates.Items(i).ToString & " - " & votes(i))
    Next
  End Sub

You cannot get the contents of the ListBox unless you iterate it.

Aethan
  • 1,986
  • 2
  • 18
  • 25
  • Okay that works! Thanks! Now I need to display the winner(s') names in a picture box. I don't know where to start with this. – Justin de Gois Sep 15 '16 at 08:48
  • That's a different story. Kindly post another question including the details on how you want the results to be presented and I'll try to answer it if I can. – Aethan Sep 15 '16 at 08:54