Advertisement
artabetes

Poll Results

Dec 12th, 2019
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Class frmPollResults
  2.   'Store widths of rectangles
  3.  'Convert percentages to whole numbers to get width of rectangle
  4.  Dim excellentWidth As Double = PercentToRectWidth(excellentPercent)
  5.   Dim veryGoodWidth As Double = PercentToRectWidth(veryGoodPercent)
  6.   Dim goodWidth As Double = PercentToRectWidth(goodPercent)
  7.   Dim satisfactoryWidth As Double = PercentToRectWidth(satisfactoryPercent)
  8.   Dim badWidth As Double = PercentToRectWidth(badPercent)
  9.  
  10.   'Store totals of each option
  11.  Dim excellentTotal As Double
  12.   Dim veryGoodTotal As Double
  13.   Dim goodTotal As Double
  14.   Dim satisfactoryTotal As Double
  15.   Dim badTotal As Double
  16.  
  17.   'Store percentages of each selection
  18.  Dim excellentPercent As Double
  19.   Dim veryGoodPercent As Double
  20.   Dim goodPercent As Double
  21.   Dim satisfactoryPercent As Double
  22.   Dim badPercent As Double
  23.  
  24.   'Keep track of total number of votes
  25.  Dim totalVotes As Double '= excellentTotal + veryGoodTotal + goodTotal + satisfactoryTotal + badTotal
  26.  
  27.   'Sub resets picture box each time vote button is clicked
  28.  'in order to refresh the graph
  29.  Private Sub ResetPictureBox(picturebox As PictureBox)
  30.     picturebox.Image = Nothing
  31.     picturebox.BackColor = Color.Empty
  32.     picturebox.Invalidate()
  33.   End Sub
  34.  
  35.   'Calculates percentages. vote represents each option's vote count. totalVotes represents total votes.
  36.  Public Function VoteCounttoPercent(vote As Double, totalVotes As Double) As Single
  37.     Return (vote \ totalVotes)
  38.   End Function
  39.  
  40.   'Convert percentage from decimal to whole number to use as rectangle width
  41.  Public Function PercentToRectWidth(percent)
  42.     Return Single.Parse(percent * 100)
  43.   End Function
  44.  
  45.   'Button click clears resets picturebox
  46.  'Tallies votes, calculates percentages
  47.  'Displays poll results in graphical format in picturebox
  48.  Private Sub btnVote_Click(sender As Object, e As EventArgs) Handles btnVote.Click
  49.     'Reset picture box prior to refreshing graph
  50.    ResetPictureBox(picPoll)
  51.  
  52.     'Add votes based on which radio button the user selects
  53.    If radExcellent.Checked = True Then
  54.       excellentTotal += 1 'If selected add 1 to count
  55.      totalVotes += 1 'If selected add 1 to count
  56.    ElseIf radVeryGood.Checked = True Then
  57.       veryGoodTotal += 1 'If selected add 1 to count
  58.      totalVotes += 1 'If selected add 1 to count
  59.    ElseIf radGood.Checked = True Then
  60.       goodTotal += 1 'If selected add 1 to count totalVotes += 1 'If selected add 1 to count
  61.      totalVotes += 1 'If selected add 1 to count
  62.    ElseIf radSatisfactory.Checked = True Then
  63.       satisfactoryTotal += 1 'If selected add 1 to count
  64.      totalVotes += 1 'If selected add 1 to count
  65.    ElseIf radBad.Checked = True Then
  66.       badTotal += 1 'If selected add 1 to count
  67.      totalVotes += 1 'If selected add 1 to count
  68.    Else
  69.       'Display message if user does not make a selection
  70.      MessageBox.Show("There appears to be some voter suppression going on. Make sure your vote gets counted.")
  71.     End If
  72.  
  73.     'Test message box
  74.    'MessageBox.Show(excellentTotal)
  75.  
  76.     'Calculates percentage each option represents of total votes
  77.    excellentPercent = VoteCounttoPercent(excellentTotal, totalVotes)
  78.     veryGoodPercent = VoteCounttoPercent(veryGoodTotal, totalVotes)
  79.     goodPercent = VoteCounttoPercent(goodTotal, totalVotes)
  80.     satisfactoryPercent = VoteCounttoPercent(satisfactoryTotal, totalVotes)
  81.     badPercent = VoteCounttoPercent(badTotal, totalVotes)
  82.  
  83.     ''Create graph in picturebox
  84.    'Dim excellentGraph As Drawing.Graphics = picPoll.CreateGraphics
  85.    'Dim Pen As New Pen(Color.Red)
  86.    ''Use percent
  87.    'excellentGraph.DrawRectangle(Pen, 10, 10, Single.Parse(excellentWidth), 10)
  88.  
  89.     'Test message box
  90.    'MessageBox.Show(FormatPercent(excellentPercent))
  91.  End Sub
  92.  
  93.   'Create graph in picturebox
  94.  Private Sub picPoll_Paint(sender As Object, e As PaintEventArgs) Handles picPoll.Paint
  95.     Using pen As New Pen(Color.Red)
  96.       e.Graphics.DrawRectangle(pen, 10, 10, excellentTotal, 10)
  97.     End Using
  98.   End Sub
  99. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement