Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class frmPollResults
- 'Store widths of rectangles
- 'Convert percentages to whole numbers to get width of rectangle
- Dim excellentWidth As Double = PercentToRectWidth(excellentPercent)
- Dim veryGoodWidth As Double = PercentToRectWidth(veryGoodPercent)
- Dim goodWidth As Double = PercentToRectWidth(goodPercent)
- Dim satisfactoryWidth As Double = PercentToRectWidth(satisfactoryPercent)
- Dim badWidth As Double = PercentToRectWidth(badPercent)
- 'Store totals of each option
- Dim excellentTotal As Double
- Dim veryGoodTotal As Double
- Dim goodTotal As Double
- Dim satisfactoryTotal As Double
- Dim badTotal As Double
- 'Store percentages of each selection
- Dim excellentPercent As Double
- Dim veryGoodPercent As Double
- Dim goodPercent As Double
- Dim satisfactoryPercent As Double
- Dim badPercent As Double
- 'Keep track of total number of votes
- Dim totalVotes As Double '= excellentTotal + veryGoodTotal + goodTotal + satisfactoryTotal + badTotal
- 'Sub resets picture box each time vote button is clicked
- 'in order to refresh the graph
- Private Sub ResetPictureBox(picturebox As PictureBox)
- picturebox.Image = Nothing
- picturebox.BackColor = Color.Empty
- picturebox.Invalidate()
- End Sub
- 'Calculates percentages. vote represents each option's vote count. totalVotes represents total votes.
- Public Function VoteCounttoPercent(vote As Double, totalVotes As Double) As Single
- Return (vote \ totalVotes)
- End Function
- 'Convert percentage from decimal to whole number to use as rectangle width
- Public Function PercentToRectWidth(percent)
- Return Single.Parse(percent * 100)
- End Function
- 'Button click clears resets picturebox
- 'Tallies votes, calculates percentages
- 'Displays poll results in graphical format in picturebox
- Private Sub btnVote_Click(sender As Object, e As EventArgs) Handles btnVote.Click
- 'Reset picture box prior to refreshing graph
- ResetPictureBox(picPoll)
- 'Add votes based on which radio button the user selects
- If radExcellent.Checked = True Then
- excellentTotal += 1 'If selected add 1 to count
- totalVotes += 1 'If selected add 1 to count
- ElseIf radVeryGood.Checked = True Then
- veryGoodTotal += 1 'If selected add 1 to count
- totalVotes += 1 'If selected add 1 to count
- ElseIf radGood.Checked = True Then
- goodTotal += 1 'If selected add 1 to count totalVotes += 1 'If selected add 1 to count
- totalVotes += 1 'If selected add 1 to count
- ElseIf radSatisfactory.Checked = True Then
- satisfactoryTotal += 1 'If selected add 1 to count
- totalVotes += 1 'If selected add 1 to count
- ElseIf radBad.Checked = True Then
- badTotal += 1 'If selected add 1 to count
- totalVotes += 1 'If selected add 1 to count
- Else
- 'Display message if user does not make a selection
- MessageBox.Show("There appears to be some voter suppression going on. Make sure your vote gets counted.")
- End If
- 'Test message box
- 'MessageBox.Show(excellentTotal)
- 'Calculates percentage each option represents of total votes
- excellentPercent = VoteCounttoPercent(excellentTotal, totalVotes)
- veryGoodPercent = VoteCounttoPercent(veryGoodTotal, totalVotes)
- goodPercent = VoteCounttoPercent(goodTotal, totalVotes)
- satisfactoryPercent = VoteCounttoPercent(satisfactoryTotal, totalVotes)
- badPercent = VoteCounttoPercent(badTotal, totalVotes)
- ''Create graph in picturebox
- 'Dim excellentGraph As Drawing.Graphics = picPoll.CreateGraphics
- 'Dim Pen As New Pen(Color.Red)
- ''Use percent
- 'excellentGraph.DrawRectangle(Pen, 10, 10, Single.Parse(excellentWidth), 10)
- 'Test message box
- 'MessageBox.Show(FormatPercent(excellentPercent))
- End Sub
- 'Create graph in picturebox
- Private Sub picPoll_Paint(sender As Object, e As PaintEventArgs) Handles picPoll.Paint
- Using pen As New Pen(Color.Red)
- e.Graphics.DrawRectangle(pen, 10, 10, excellentTotal, 10)
- End Using
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement