DotNet Tutorials

Server Intellect

 Creating Polling System in Windows Form with C#

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

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>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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 void butSubmit_Click(object sender, EventArgs e)
{
if (radMcCain.Checked == true)
submitVote("McCain");
else if (radObama.Checked == true)
submitVote("Obama");
}

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 void submitVote(string theVote)
{
try
{
XDocument xmlDoc = 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.";
}
}

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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 void readXML()
{
XDocument xmlDoc = XDocument.Load("Poll.xml");

var votes = from vote in xmlDoc.Descendants("Vote")
select new
{
Vote = vote.Element("Choice").Value,
};

int mCount;
int oCount;
mCount = 0;
oCount = 0;

foreach (var vote in votes)
{
if (vote.Vote == "McCain")
mCount++;
else if (vote.Vote == "Obama")
oCount++;
}

double theTotal;
theTotal = mCount + oCount;
double mPercent;
double oPercent;
mPercent = (mCount/theTotal)*100;
oPercent = (oCount/theTotal)*100;

lblResults.Text = "McCain: " + mCount + " votes (" + mPercent + "%).\n";
lblResults.Text = lblResults.Text + "Obama: " + oCount + " votes (" + oPercent + "%).\n\n";
}

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.

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:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace WinForm_Poll_cs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void submitVote(string theVote)
{
try
{
XDocument xmlDoc = 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.";
}
}

private void butResults_Click(object sender, EventArgs e)
{
readXML();
}

private void readXML()
{
XDocument xmlDoc = XDocument.Load("Poll.xml");

var votes = from vote in xmlDoc.Descendants("Vote")
select new
{
Vote = vote.Element("Choice").Value,
};

int mCount;
int oCount;
mCount = 0;
oCount = 0;

foreach (var vote in votes)
{
if (vote.Vote == "McCain")
mCount++;
else if (vote.Vote == "Obama")
oCount++;
}

double theTotal;
theTotal = mCount + oCount;
double mPercent;
double oPercent;
mPercent = (mCount/theTotal)*100;
oPercent = (oCount/theTotal)*100;

lblResults.Text = "McCain: " + mCount + " votes (" + mPercent + "%).\n";
lblResults.Text = lblResults.Text + "Obama: " + oCount + " votes (" + oPercent + "%).\n\n";
}

private void butSubmit_Click(object sender, EventArgs e)
{
if (radMcCain.Checked == true)
submitVote("McCain");
else if (radObama.Checked == true)
submitVote("Obama");
}
}
}

Looking for the VB.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