DotNet Tutorials

Server Intellect

 Creating Polling System in Windows Form with VB

This tutorial will show you how to develop a voting/polling system in a Windows Form with the use of XML and LINQ, using VB.

Polling systems can be extremely useful for capturing opinions or current trends. In this tutorial, we will look at how we can create a Windows Form to build a voting system where users can vote who is their favorite Presidentail Candidate, and then see the current results. We will be using an XML file to store the data. We will start by creating the structure of the XML file:

<?xml version="1.0" encoding="utf-8"?>
<Poll>
<Vote>
<Choice>Obama</Choice>
</Vote>
<Vote>
<Choice>McCain</Choice>
</Vote>
</Poll>

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

Next, we can design our Form; we will have a label citing the question, and then the two options as Radio Buttons, and then a button to submit the vote. All of these controls can be grouped together. And then underneath, we can put another button to show current results of the poll, and a label to output the results.

Once we have designed our form, we can double-click on the Submit button to create a handler for it in the code-behind:

Private Sub butSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSubmit.Click
If radMcCain.Checked = True Then
submitVote("McCain")
ElseIf radObama.Checked = True Then
submitVote("Obama")
End If
End Sub

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

On button click, we check to see which option has been chosen, and then we call a method to commit the vote to the XML file. The submitVote method looks something like this:

Private Sub submitVote(ByVal theVote As String)
Try
Dim xmlDoc As XDocument = XDocument.Load("Poll.xml")

xmlDoc.Element("Poll").Add(New XElement("Vote", New XElement("Choice", theVote)))

xmlDoc.Save("Poll.xml")
lblResults.Text = "Thank you for your vote."
readXML()
Catch
lblResults.Text = "Sorry, unable to process request. Please try again."
End Try
End Sub

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

Here, we are making use of a Try..Catch to try and reduce errors shown to the user. We are then opening the XML document and using LINQ to add a new element to it, saving the newly-cast vote into the document. Finally, we call the readXML method to output current results to the form:

Private Sub readXML()
Dim xmlDoc As XDocument = XDocument.Load("Poll.xml")

Dim votes = From vote In xmlDoc.Descendants("Vote") _
Select Vote = vote.Element("Choice").Value

Dim mCount As Integer
Dim oCount As Integer
mCount = 0
oCount = 0

For Each vote In votes
If vote = "McCain" Then
mCount += 1
ElseIf vote = "Obama" Then
oCount += 1
End If
Next vote

Dim theTotal As Double
theTotal = mCount + oCount
Dim mPercent As Double
Dim oPercent As Double
mPercent = (mCount / theTotal) * 100
oPercent = (oCount / theTotal) * 100

lblResults.Text = "McCain: " & mCount & " votes (" & mPercent & "%)." & Constants.vbLf
lblResults.Text = lblResults.Text & "Obama: " & oCount & " votes (" & oPercent & "%)." & Constants.vbLf + Constants.vbLf
End Sub

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

This method also makes use of LINQ to select all data from the XML file and then loop through it to count all votes for each candidate. Then we perform a simple calculation to determine the percentage of votes for each. Finally, we output the results to the form.
The entire code-behind will look something like this:

Public Class Form1

Private Sub butSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSubmit.Click
If radMcCain.Checked = True Then
submitVote("McCain")
ElseIf radObama.Checked = True Then
submitVote("Obama")
End If
End Sub

Private Sub submitVote(ByVal theVote As String)
Try
Dim xmlDoc As XDocument = XDocument.Load("Poll.xml")

xmlDoc.Element("Poll").Add(New XElement("Vote", New XElement("Choice", theVote)))

xmlDoc.Save("Poll.xml")
lblResults.Text = "Thank you for your vote."
readXML()
Catch
lblResults.Text = "Sorry, unable to process request. Please try again."
End Try
End Sub

Private Sub readXML()
Dim xmlDoc As XDocument = XDocument.Load("Poll.xml")

Dim votes = From vote In xmlDoc.Descendants("Vote") _
Select Vote = vote.Element("Choice").Value

Dim mCount As Integer
Dim oCount As Integer
mCount = 0
oCount = 0

For Each vote In votes
If vote = "McCain" Then
mCount += 1
ElseIf vote = "Obama" Then
oCount += 1
End If
Next vote

Dim theTotal As Double
theTotal = mCount + oCount
Dim mPercent As Double
Dim oPercent As Double
mPercent = (mCount / theTotal) * 100
oPercent = (oCount / theTotal) * 100

lblResults.Text = "McCain: " & mCount & " votes (" & mPercent & "%)." & Constants.vbLf
lblResults.Text = lblResults.Text & "Obama: " & oCount & " votes (" & oPercent & "%)." & Constants.vbLf + Constants.vbLf
End Sub

Private Sub butResults_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butResults.Click
readXML()
End Sub
End Class

Looking for the C#.NET 2008 Version? Click Here!

Looking for more .NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect