Advertisement
Guest User

Voting Machine v3

a guest
Jul 14th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. Imports System.IO
  2.  
  3. Public Class Voting
  4.  
  5. Dim Candidates() As String
  6. Dim clickCount As Integer = 0
  7. Dim candidateVotes() As String
  8. Dim votes() As Integer
  9.  
  10.  
  11. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  12.  
  13. 'Get default values and views for Voting Machine
  14. resultsList.Visible = False
  15. candidateList.Location = New Point(143, 71)
  16. tallyVotes.Enabled = False
  17. resultsList.Enabled = False
  18. Label1.Text = "Click 'Nominate Candidate' to enter a candidate, or 'Start Voting'" & vbCrLf & "to end nominations and start the voting."
  19.  
  20. End Sub
  21.  
  22. Private Sub candidateList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles candidateList.SelectedIndexChanged
  23.  
  24. End Sub
  25.  
  26. Private Sub resultsList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles resultsList.SelectedIndexChanged
  27.  
  28. End Sub
  29.  
  30. Private Sub nominateCandidate_Click(sender As Object, e As EventArgs) Handles nominateCandidate.Click
  31.  
  32. 'Instructions
  33. MessageBox.Show("When finished entering candidates, simply press enter on a blank line.", "Instructions")
  34.  
  35. 'Gather list of Candidates
  36. Dim candidateName = InputBox("Enter the name of your candidate (first and last name please):", "Candidate Name", "", 500, 500)
  37. Dim i As Integer = 0
  38. 'Loops until a null string is entered signaling the end of input
  39. Do Until String.IsNullOrEmpty(candidateName)
  40. ReDim Preserve Candidates(i)
  41. Candidates(i) = candidateName
  42. i += 1
  43. candidateName = InputBox("Enter the name of your candidate (first and last name please):", "Candidate Name", "", 500, 500)
  44. Loop
  45.  
  46. End Sub
  47.  
  48. Private Sub startVoting_Click(sender As Object, e As EventArgs) Handles startVoting.Click
  49.  
  50. 'Disable the Nomination button
  51. nominateCandidate.Enabled = False
  52.  
  53. 'Enable the tally votes button
  54. tallyVotes.Enabled = True
  55.  
  56. 'Set the label text to give instructions for tallying votes.
  57. Label1.Text = "Vote for a candidate by double-clicking his or her name." & vbCrLf & "End the voting by clicking on 'Tally Votes'."
  58.  
  59. 'Call sub to display the candidates for voting
  60. showCandidates()
  61.  
  62. End Sub
  63.  
  64. Private Sub tallyVotes_Click(sender As Object, e As EventArgs) Handles tallyVotes.Click
  65.  
  66. 'Makes results listbox visible and moves the candidate list to the left
  67. resultsList.Visible = True
  68. candidateList.Location = New Point(14, 71)
  69.  
  70. 'Call the sub to tally the votes and display the winner
  71. getResults()
  72.  
  73. End Sub
  74.  
  75. Private Sub candidateList_DoubleClick(sender As Object, e As EventArgs) Handles candidateList.DoubleClick
  76.  
  77. 'Selected candidate gets a vote tallied
  78. Try
  79. 'Selected candidate gets a vote tallied
  80. votes(candidateList.SelectedIndex) += 1
  81. candidateList.SelectedIndex = -1
  82. Catch exc As IndexOutOfRangeException
  83. MessageBox.Show("Please click on a candidate to vote.", "Attention!")
  84. End Try
  85.  
  86. End Sub
  87.  
  88. Sub showCandidates()
  89.  
  90. 'Display the candidates in the listbox and sort alphabetically by last name
  91. Dim query = From candidate In Candidates
  92. Let firstName = candidate.Split(" "c)(0)
  93. Let lastName = candidate.Split(" "c)(1)
  94. Let name = firstName & " " & lastName
  95. Order By lastName
  96. Select name
  97.  
  98. For Each Name As String In query
  99. candidateList.Items.Add(Name)
  100. Next
  101.  
  102. ReDim Preserve votes(candidateList.Items.Count)
  103.  
  104. End Sub
  105.  
  106. Sub getResults()
  107.  
  108. 'Add the results to the Results Listbox
  109. For Each i In votes
  110. resultsList.Items.Add(i)
  111. Next
  112.  
  113. 'Declare Winner
  114.  
  115.  
  116. End Sub
  117.  
  118. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement