VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code

Polling System

Interactive survey tool built with Visual Basic - VB6 and VB.NET


Survey and polling tools are often used in marketing or politics to assess ratings for certain services or products. Polling tools can take many forms, from a simple dichotomous scale of Yes and No, to a more complex Likert Scale that consists of three or more choices.

You can create a polling tool in Visual Basic easily using option buttons. In our program, users are given five choices: Excellent, Very Good, Good, Satisfactory, and Bad. The results are presented in both frequency and percentage formats.

The graphical display of percentages is implemented using the Line method. The syntax to draw the rectangular bar in a picture box is:

Picture1.Line (x1, y1)-(x2, y2), color, BF

Where (x₁,y₁) is the coordinates of the upper left corner of the bar and (x₂,y₂) is the coordinates of the lower right corner of the bar.

Interactive Demo

Try the polling system below. Select your rating and click "Vote" to see the results.

How would you rate our Visual Basic Sample Code?

Poll Results

Total Votes: 0

Visual Basic Code Samples

This VB6 code implements the polling system with graphical display of results:

Dim total, Excel_total, VG_total, G_total, Sat_total, Bad_total As Integer
Dim Excel_percent, VG_percent, G_percent, Sat_percent, Bad_percent As Single
Dim done As Boolean

Private Sub cmd_Vote_Click()
    Picture1.Cls
    
    If Option_Excel.Value = True Then
        Excel_total = Excel_total + 1
        Lbl_ExcelTotal = Excel_total
    ElseIf Option_VG.Value = True Then
        VG_total = VG_total + 1
        Lbl_VGTotal = VG_total
    ElseIf Option_G.Value = True Then
        G_total = G_total + 1
        Lbl_GTotal = G_total
    ElseIf Option_Sat.Value = True Then
        Sat_total = Sat_total + 1
        Lbl_SatTotal = Sat_total
    ElseIf Option_Bad.Value = True Then
        Bad_total = Bad_total + 1
        Lbl_BadTotal = Bad_total
    End If
    
    total = Excel_total + VG_total + G_total + Sat_total + Bad_total
    Lbl_Total = total
    
    Excel_percent = Excel_total / total
    VG_percent = VG_total / total
    G_percent = G_total / total
    Sat_percent = Sat_total / total
    Bad_percent = Bad_total / total
    
    Lbl_Excel.Caption = Format(Excel_percent, "Percent")
    Lbl_VG.Caption = Format(VG_percent, "Percent")
    Lbl_G.Caption = Format(G_percent, "Percent")
    Lbl_Sat.Caption = Format(Sat_percent, "Percent")
    Lbl_Bad.Caption = Format(Bad_percent, "Percent")
    
    ' Draw bars for each rating
    Picture1.Line (100, 750)-(3800 * Excel_percent, 950), vbRed, BF
    Picture1.Line (100, 1450)-(3800 * VG_percent, 1650), vbMagenta, BF
    Picture1.Line (100, 2150)-(3800 * G_percent, 2350), vbGreen, BF
    Picture1.Line (100, 2850)-(3800 * Sat_percent, 3050), vbBlue, BF
    Picture1.Line (100, 3550)-(3800 * Bad_percent, 3750), vbYellow, BF
End Sub

This VB.NET code implements the same polling system using modern .NET features:

Public Class PollingSystem
    Private totalVotes, excellentVotes, veryGoodVotes, goodVotes, satisfactoryVotes, badVotes As Integer
    
    Private Sub btnVote_Click(sender As Object, e As EventArgs) Handles btnVote.Click
        If rbExcellent.Checked Then
            excellentVotes += 1
        ElseIf rbVeryGood.Checked Then
            veryGoodVotes += 1
        ElseIf rbGood.Checked Then
            goodVotes += 1
        ElseIf rbSatisfactory.Checked Then
            satisfactoryVotes += 1
        ElseIf rbBad.Checked Then
            badVotes += 1
        End If
        
        totalVotes = excellentVotes + veryGoodVotes + goodVotes + satisfactoryVotes + badVotes
        UpdateResults()
    End Sub
    
    Private Sub UpdateResults()
        ' Update labels with counts
        lblExcellentCount.Text = excellentVotes.ToString()
        lblVeryGoodCount.Text = veryGoodVotes.ToString()
        lblGoodCount.Text = goodVotes.ToString()
        lblSatisfactoryCount.Text = satisfactoryVotes.ToString()
        lblBadCount.Text = badVotes.ToString()
        lblTotalVotes.Text = totalVotes.ToString()
        
        ' Calculate percentages
        Dim excellentPercent = excellentVotes / totalVotes
        Dim veryGoodPercent = veryGoodVotes / totalVotes
        Dim goodPercent = goodVotes / totalVotes
        Dim satisfactoryPercent = satisfactoryVotes / totalVotes
        Dim badPercent = badVotes / totalVotes
        
        ' Update percentage labels
        lblExcellentPercent.Text = excellentPercent.ToString("P1")
        lblVeryGoodPercent.Text = veryGoodPercent.ToString("P1")
        lblGoodPercent.Text = goodPercent.ToString("P1")
        lblSatisfactoryPercent.Text = satisfactoryPercent.ToString("P1")
        lblBadPercent.Text = badPercent.ToString("P1")
        
        ' Draw the bar chart
        DrawBarChart()
    End Sub
    
    Private Sub DrawBarChart()
        Dim g As Graphics = picChart.CreateGraphics()
        g.Clear(Color.White)
        
        Dim barWidth = picChart.Width - 40
        Dim barHeight = 20
        Dim spacing = 30
        Dim y = 10
        
        ' Draw Excellent bar
        Dim excellentWidth = barWidth * (excellentVotes / totalVotes)
        g.FillRectangle(Brushes.Red, 20, y, excellentWidth, barHeight)
        y += spacing
        
        ' Draw Very Good bar
        Dim veryGoodWidth = barWidth * (veryGoodVotes / totalVotes)
        g.FillRectangle(Brushes.Magenta, 20, y, veryGoodWidth, barHeight)
        y += spacing
        
        ' Draw Good bar
        Dim goodWidth = barWidth * (goodVotes / totalVotes)
        g.FillRectangle(Brushes.Green, 20, y, goodWidth, barHeight)
        y += spacing
        
        ' Draw Satisfactory bar
        Dim satisfactoryWidth = barWidth * (satisfactoryVotes / totalVotes)
        g.FillRectangle(Brushes.Blue, 20, y, satisfactoryWidth, barHeight)
        y += spacing
        
        ' Draw Bad bar
        Dim badWidth = barWidth * (badVotes / totalVotes)
        g.FillRectangle(Brushes.Yellow, 20, y, badWidth, barHeight)
    End Sub
End Class

Polling System Interface

The original VB6 interface for the polling system:

Polling System Interface

Key Features of the Polling System

Likert Scale Options

Five-point rating scale from Excellent to Bad for nuanced feedback

Visual Results

Color-coded bar charts to visualize distribution of responses

Percentage Calculation

Automatic calculation of response percentages

Real-time Updates

Results update instantly after each vote