pp/5/Candidate.cs 1.1 KB (1119b); Sat, 04 Jun 2011 21:49
´╗┐using System;
using System.Windows.Forms;

namespace Cselian.Voting
{
	public partial class Candidate : UserControl, ICandidate
	{
		public static event EventHandler CountChanged;

		public static int Compare(ICandidate x, ICandidate y)
		{
			return x.Votes.CompareTo(y.Votes) * -10
				+ x.PersonName.CompareTo(y.PersonName);
		}

		public Candidate(string name, int votes)
		{
			InitializeComponent();
			PersonName = name;
			Votes = votes;
			UpdateUI();
		}


		#region ICandidate Members

		public string PersonName
		{
			get;
			private set;
		}

		public int Votes
		{
			get;
			private set;
		}

		public void Clear()
		{
			Votes = 0;
			UpdateUI();
		}

		public override string ToString()
		{
			return Votes.ToString("00") + " " + PersonName;
		}

		#endregion

		private void NameCtl_Click(object sender, EventArgs e)
		{
			Votes += 1;
			if (null != CountChanged) CountChanged(this, EventArgs.Empty);
			UpdateUI();
		}

		private void UpdateUI()
		{
			NameCtl.Text = PersonName;
			CountCtl.Text = Votes.ToString("00");
		}
	}
}