Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListBox Problem 2

Status
Not open for further replies.

jfarrell

Technical User
Mar 3, 2001
15
US
I have a ListBox problem I have been laboring over and
cannot find the solution. I would appreciate any help
given. The specs are for two List boxes. The left list
box has the name of candidates. When someone clicks on a
candidates name it records his vote. When a Tally Vote
command button is clicked, a second list box shows the
number of votes received by each candidate, should appear
alongside the first list box. Also the candidates with
the bigest number of votes should be displayed in a picture box.
Can anyone point me in the right direction because this
problem has lost me.
Many thanks in advance.
Jack
 
I would declare an integer array to hold the number of votes for each candidate, and synchronize it with the list items. Whenever a candidate is selected in the list, increment the cooresponding array element to record another vote for that candidate.

-----------------------------
'General/Declarations of form:
Private Votes() As Integer

'After the list is loaded with candidates:
ReDim Votes(List1.ListCount) As Integer

Private Sub List1_Click()
Votes(List1.ListIndex) = Votes(List1.ListIndex) + 1
End Sub

Private Sub cmdTallyVotes_Click()
Dim i As Long
Dim MostVotes As Integer
Dim MostVotesIndex As Integer

For i = 0 to UBound(Votes)
List2.AddItem Votes(i)
If Votes(i) > MostVotes Then
MostVotes = Votes(i)
MostVotesIndex = i
Next i

MsgBox List1.List(MostVotesIndex) & " has the most votes with " & MostVotes & " votes.", vbInformation

End Sub

Hope that helps.

~Mike
Any man willing to sacrifice liberty for security deserves neither liberty nor security.

-Ben Franklin
 
Mike, can you help with one adjustment. The counts are not comming out correct.
I probably have the refresh in the wrong place. i did the code using your help. It was
a great help. For this I thank you.

Private Sub cmdCandidate_Click()
lstCandidate.AddItem txtCandidate.Text
ReDim Votes(lstCandidate.ListCount) As Integer
txtCandidate.Text = ""
txtCandidate.SetFocus

End Sub

Private Sub cmdTally_Click()
Dim i As Long
Dim MostVotes As Integer
Dim MostVotesIndex As Integer
For i = 0 To UBound(Votes)
lstVotes.AddItem Votes(i)
If Votes(i) > MostVotes Then
MostVotes = Votes(i)
MostVotesIndex = i
End If
Next i

picVotes.Print lstCandidate.List(MostVotesIndex) & " with " & MostVotes & " votes."
End Sub

Private Sub lstCandidate_Click()
Votes(lstCandidate.ListIndex) = Votes(lstCandidate.ListIndex) + 1
End Sub

PS. Modern Day Benjamin Franklin - Congressman Ron Paul of Texas.
 
Hi Jack,
First off when you are adding a candidate you might want to use Redim Preserve Votes(lstCandidate.ListCount) so as to preserve the votes already obtained by the candidates that are already there. Also try changing the for statement in the CmdTally_Click to have i initialise to 1 instead of 0 and see if that corrects the error in the numbers your getting. Hope this helps a bit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top