Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Public Class Voting
- Dim Candidates() As String
- Dim clickCount As Integer = 0
- Dim candidateVotes() As String
- Dim votes() As Integer
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- 'Get default values and views for Voting Machine
- resultsList.Visible = False
- candidateList.Location = New Point(143, 71)
- tallyVotes.Enabled = False
- resultsList.Enabled = False
- Label1.Text = "Click 'Nominate Candidate' to enter a candidate, or 'Start Voting'" & vbCrLf & "to end nominations and start the voting."
- End Sub
- Private Sub candidateList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles candidateList.SelectedIndexChanged
- End Sub
- Private Sub resultsList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles resultsList.SelectedIndexChanged
- End Sub
- Private Sub nominateCandidate_Click(sender As Object, e As EventArgs) Handles nominateCandidate.Click
- 'Instructions
- MessageBox.Show("When finished entering candidates, simply press enter on a blank line.", "Instructions")
- 'Gather list of Candidates
- Dim candidateName = InputBox("Enter the name of your candidate (first and last name please):", "Candidate Name", "", 500, 500)
- Dim i As Integer = 0
- 'Loops until a null string is entered signaling the end of input
- Do Until String.IsNullOrEmpty(candidateName)
- ReDim Preserve Candidates(i)
- Candidates(i) = candidateName
- i += 1
- candidateName = InputBox("Enter the name of your candidate (first and last name please):", "Candidate Name", "", 500, 500)
- Loop
- End Sub
- Private Sub startVoting_Click(sender As Object, e As EventArgs) Handles startVoting.Click
- 'Disable the Nomination button
- nominateCandidate.Enabled = False
- 'Enable the tally votes button
- tallyVotes.Enabled = True
- 'Set the label text to give instructions for tallying votes.
- Label1.Text = "Vote for a candidate by double-clicking his or her name." & vbCrLf & "End the voting by clicking on 'Tally Votes'."
- 'Call sub to display the candidates for voting
- showCandidates()
- End Sub
- Private Sub tallyVotes_Click(sender As Object, e As EventArgs) Handles tallyVotes.Click
- 'Makes results listbox visible and moves the candidate list to the left
- resultsList.Visible = True
- candidateList.Location = New Point(14, 71)
- 'Call the sub to tally the votes and display the winner
- getResults()
- End Sub
- Private Sub candidateList_DoubleClick(sender As Object, e As EventArgs) Handles candidateList.DoubleClick
- 'Selected candidate gets a vote tallied
- Try
- 'Selected candidate gets a vote tallied
- votes(candidateList.SelectedIndex) += 1
- candidateList.SelectedIndex = -1
- Catch exc As IndexOutOfRangeException
- MessageBox.Show("Please click on a candidate to vote.", "Attention!")
- End Try
- End Sub
- Sub showCandidates()
- 'Display the candidates in the listbox and sort alphabetically by last name
- Dim query = From candidate In Candidates
- Let firstName = candidate.Split(" "c)(0)
- Let lastName = candidate.Split(" "c)(1)
- Let name = firstName & " " & lastName
- Order By lastName
- Select name
- For Each Name As String In query
- candidateList.Items.Add(Name)
- Next
- ReDim Preserve votes(candidateList.Items.Count)
- End Sub
- Sub getResults()
- 'Add the results to the Results Listbox
- For Each i In votes
- resultsList.Items.Add(i)
- Next
- 'Declare Winner
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement