Code Snippet:
http://msdn.microsoft.com/en-us/library/z41h7fat.aspx
CodePlex.Snippets 4.0 - Visual Studio 2010 Code Snippets for C# Developers
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace WFControlLibrary { public partial class RadioButtonFive : UserControl { public event EventHandler Clicked; public int AnswerWidth { get; set; } public int RowNumber = 0; public int Value { get; set; } public int AnswerTypes { get; set; } // 1 = Five; 2 = Two; 3 = Two Numbers [Bindable(true)] public int SerialNo { get; set; } [Bindable(true)] public string ItemCode { get; set; } [Bindable(true)] public string Question { get; set; } [Bindable(true)] public decimal AssignedValue { get; set; } [Bindable(true)] public decimal Score { get; set; } public decimal NumberAInput { get; set; } public decimal NumberBInput { get; set; } [Bindable(true)] public bool AllowDataChanged { get; set; } public int Col1Width { get; set; } public int Col2Width { get; set; } public int Col3Width { get; set; } public int Col4Width { get; set; } public int Col5Width { get; set; } TextBox NumberA = new TextBox(); TextBox NumberB = new TextBox(); ErrorProvider errorProvider = new ErrorProvider(); public RadioButtonFive() { InitializeComponent(); //tableLayoutPanel1.co tableLayoutPanel1.BackColor = Color.LightPink; if (RowNumber % 2 == 0) { //tableLayoutPanel1.BackColor = new Color( } } public RadioButtonFive(int aSerialNo, string itemCode, string aQuestion, decimal aValue, decimal aScore, int questionWidth) { InitializeComponent(); this.SerialNo = aSerialNo; this.ItemCode = itemCode; this.Question = aQuestion; this.AssignedValue = aValue; this.Score = aScore; lbSerialNo.Text = SerialNo.ToString(); lbQuestion.Text = Question; lbValue.Text = AssignedValue.ToString(); lbScore.Text = Score.ToString(); Col2Width = questionWidth; ErrorProvider errorProvider = new ErrorProvider(); if (aSerialNo % 2 == 0) { tableLayoutPanel1.BackColor = Color.Beige; } } public RadioButtonFive(int aSerialNo, string itemCode, string aQuestion, decimal aValue, decimal aScore, int questionWidth, int type) { InitializeComponent(); this.SerialNo = aSerialNo; this.ItemCode = itemCode; this.Question = aQuestion; this.AssignedValue = aValue; this.Score = aScore; lbSerialNo.Text = SerialNo.ToString(); lbQuestion.Text = Question; lbValue.Text = AssignedValue.ToString(); lbScore.Text = Score.ToString(); Col2Width = questionWidth; if (aSerialNo % 2 == 0) { tableLayoutPanel1.BackColor = Color.Beige; } } public new void Refresh() { lbSerialNo.Text = SerialNo.ToString(); lbQuestion.Text = Question; lbValue.Text = AssignedValue.ToString(); lbScore.Text = Score.ToString(); tableLayoutPanel1.ColumnStyles[1].SizeType = SizeType.Absolute; tableLayoutPanel1.ColumnStyles[1].Width = Col2Width; if (AnswerTypes == 1) { if (Score > 0.0001m) { int x = (int)Math.Round(((4 * Score) / AssignedValue)); if (x == 4) { rbA.Checked = true; } else if (x == 3) { rbB.Checked = true; } else if (x == 2) { rbC.Checked = true; } else if (x == 1) { rbD.Checked = true; } else { rbE.Checked = true; } } if (AllowDataChanged == false) { rbA.Enabled = false; rbB.Enabled = false; rbC.Enabled = false; rbD.Enabled = false; rbE.Enabled = false; } } if (AnswerTypes == 2) { if (Score > 0.0001m) { int x = (int)Math.Round((Score / AssignedValue)); if (x == 1) { rbA.Checked = true; } else { rbB.Checked = true; } } if (AllowDataChanged == false) { rbA.Enabled = false; rbB.Enabled = false; } } if (AnswerTypes == 3) { NumberA.Text = NumberAInput.ToString(); NumberA.Name = "txtNumber"; NumberB.Text = NumberBInput.ToString(); NumberB.Name = "txtExpiredNumber"; if (AllowDataChanged == false) { NumberA.Enabled = false; NumberB.Enabled = false; } NumberA.TextChanged += new EventHandler(NumberA_TextChanged); NumberB.TextChanged += new EventHandler(NumberB_TextChanged); } } private void rbA_CheckedChanged(object sender, EventArgs e) { if (AnswerTypes == 1) { if (rbA.Checked) { Value = 4; } if (rbB.Checked) { Value = 3; } if (rbC.Checked) { Value = 2; } if (rbD.Checked) { Value = 1; } if (rbE.Checked) { Value = 0; } Score = Value * AssignedValue / 4; lbScore.Text = Score.ToString(); } if (AnswerTypes == 2) { if (rbA.Checked) { Value = 1; } if (rbB.Checked) { Value = 0; } Score = Value * AssignedValue; lbScore.Text = Score.ToString(); } if (Value == 0) { tableLayoutPanel1.BackColor = Color.LightPink; } else { tableLayoutPanel1.BackColor = Color.LawnGreen; } Clicked(sender, e); } public void ProperAnswerType() { if (AnswerTypes == 1) //default { return; } if (AnswerTypes == 2) // two answer => modify answers { rbC.Visible = false; rbD.Visible = false; rbE.Visible = false; rbC.Enabled = false; rbD.Enabled = false; rbE.Enabled = false; rbA.Text = "达标"; rbB.Text = "未达标"; TableLayoutColumnStyleCollection styles = tableFlowLayoutPanelAnswer.ColumnStyles; if (styles.Count == 5) { styles[0].Width = AnswerWidth/2 - 1; styles[1].Width = AnswerWidth/2 - 1; styles[2].Width = 0; styles[3].Width = 0; styles[4].Width = 0; } TableLayoutColumnStyleCollection styles2 = tableLayoutPanel1.ColumnStyles; styles2[3].Width = AnswerWidth; } if (AnswerTypes == 3) // numbers { rbA.Visible = false; rbB.Visible = false; rbC.Visible = false; rbD.Visible = false; rbE.Visible = false; rbA.Enabled = false; rbB.Enabled = false; rbC.Enabled = false; rbD.Enabled = false; rbE.Enabled = false; //NumberA.TextChanged += NumberA_TextChanged; //NumberB.TextChanged += NumberB_TextChanged; NumberA.Validated += NumberA_TextChanged; NumberB.Validated += NumberB_TextChanged; NumberA.Width = AnswerWidth/2 - 5; NumberB.Width = AnswerWidth/2 - 5; TableLayoutColumnStyleCollection styles = tableFlowLayoutPanelAnswer.ColumnStyles; if (styles.Count == 5) { styles[0].Width = AnswerWidth/2 - 1; styles[1].Width = AnswerWidth/2 - 1; styles[2].Width = 0; styles[3].Width = 0; styles[4].Width = 0; } tableFlowLayoutPanelAnswer.Controls.Clear(); tableFlowLayoutPanelAnswer.Controls.Add(NumberA, 0, 0); tableFlowLayoutPanelAnswer.Controls.Add(NumberB, 1, 0); TableLayoutColumnStyleCollection styles2 = tableLayoutPanel1.ColumnStyles; styles2[3].Width = AnswerWidth; } } void NumberA_TextChanged(object sender, EventArgs e) { if (string.IsNullOrEmpty(NumberA.Text.Trim())) { NumberAInput = decimal.MinValue; //special Clicked(sender, e); return; } decimal tmp; if (decimal.TryParse(NumberA.Text.ToString(), out tmp)) { NumberAInput = tmp; if (NumberAInput > 0 && NumberBInput >= 0 && NumberAInput - NumberBInput >= 0) { NumberAInput = tmp; Score = Math.Round((NumberAInput - NumberBInput) / NumberAInput, 3) * AssignedValue; lbScore.Text = Score.ToString(); errorProvider.SetError(NumberA, ""); } else { lbScore.Text = ""; errorProvider.SetError(NumberA, "该行数据有错"); } } else { NumberAInput = 0; lbScore.Text = ""; errorProvider.SetError(NumberA, "该值不是一个数据。"); } if (Score == 0) { tableLayoutPanel1.BackColor = Color.LightPink; } else { tableLayoutPanel1.BackColor = Color.LawnGreen; } Clicked(sender, e); } void NumberB_TextChanged(object sender, EventArgs e) { if (string.IsNullOrEmpty(NumberA.Text.Trim())) { NumberAInput = decimal.MinValue;//special Clicked(sender, e); return; } decimal tmp; if (decimal.TryParse(NumberB.Text.ToString(), out tmp)) { if (NumberAInput > 0 && NumberBInput >= 0 && NumberAInput - NumberBInput >= 0) { NumberBInput = tmp; Score = Math.Round((NumberAInput - NumberBInput) / NumberAInput, 3) * AssignedValue; lbScore.Text = Score.ToString(); errorProvider.SetError(NumberB, ""); } else { errorProvider.SetError(NumberB, "该行数据有错"); lbScore.Text = ""; } } else { NumberBInput = 0; lbScore.Text = ""; MessageBox.Show("该值不是一个数据。"); } if (Score == 0) { tableLayoutPanel1.BackColor = Color.LightPink; } else { tableLayoutPanel1.BackColor = Color.LawnGreen; } Clicked(sender, e); } } }