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>
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.<?xml version="1.0" encoding="utf-8"?>
<Poll>
<Vote>
<Vote>
</Poll><Choice>Obama</Choice>
</Vote><Vote>
<Choice>McCain</Choice>
</Vote>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
{
catch
{
}{
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();
}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.";
}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")
int mCount;
int oCount;
mCount = 0;
oCount = 0;
foreach (var vote in votes)
{
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";
}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";
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()
{
private void submitVote(string theVote)
{
private void butResults_Click(object sender, EventArgs e)
{
private void readXML()
{
private void butSubmit_Click(object sender, EventArgs e)
{
}{
InitializeComponent();
}private void submitVote(string theVote)
{
try
{
catch
{
}{
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();
}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")
int mCount;
int oCount;
mCount = 0;
oCount = 0;
foreach (var vote in votes)
{
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";
}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");
Comments (0)
Post a Comment