Code Snippet

时间:2023-03-09 05:31:40
Code Snippet

Code Snippet:

  http://msdn.microsoft.com/en-us/library/z41h7fat.aspx

CodePlex.Snippets 4.0 - Visual Studio 2010 Code Snippets for C# Developers

  http://blogs.msdn.com/b/dohollan/archive/2010/07/12/codeplex-snippets-4-0-visual-studio-2010-code-snippets-for-c-developers.aspx

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 = ;
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 % == )
{
//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 % == )
{
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 % == )
{
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[].SizeType = SizeType.Absolute;
tableLayoutPanel1.ColumnStyles[].Width = Col2Width; if (AnswerTypes == )
{
if (Score > 0.0001m)
{
int x = (int)Math.Round((( * Score) / AssignedValue)); if (x == )
{
rbA.Checked = true;
}
else if (x == )
{
rbB.Checked = true;
}
else if (x == )
{
rbC.Checked = true;
}
else if (x == )
{
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 == )
{
if (Score > 0.0001m)
{
int x = (int)Math.Round((Score / AssignedValue)); if (x == )
{
rbA.Checked = true;
}
else
{
rbB.Checked = true;
}
} if (AllowDataChanged == false)
{
rbA.Enabled = false;
rbB.Enabled = false;
} } if (AnswerTypes == )
{
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 == )
{
if (rbA.Checked)
{
Value = ;
}
if (rbB.Checked)
{
Value = ;
}
if (rbC.Checked)
{
Value = ;
}
if (rbD.Checked)
{
Value = ;
}
if (rbE.Checked)
{
Value = ;
} Score = Value * AssignedValue / ; lbScore.Text = Score.ToString();
} if (AnswerTypes == )
{
if (rbA.Checked)
{
Value = ;
}
if (rbB.Checked)
{
Value = ;
} Score = Value * AssignedValue; lbScore.Text = Score.ToString();
} if (Value == )
{
tableLayoutPanel1.BackColor = Color.LightPink;
}
else
{
tableLayoutPanel1.BackColor = Color.LawnGreen;
} Clicked(sender, e);
} public void ProperAnswerType()
{
if (AnswerTypes == ) //default
{
return;
} if (AnswerTypes == ) // 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 == )
{
styles[].Width = AnswerWidth/ - ;
styles[].Width = AnswerWidth/ - ;
styles[].Width = ;
styles[].Width = ;
styles[].Width = ;
} TableLayoutColumnStyleCollection styles2 = tableLayoutPanel1.ColumnStyles;
styles2[].Width = AnswerWidth;
} if (AnswerTypes == ) // 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/ - ;
NumberB.Width = AnswerWidth/ - ; TableLayoutColumnStyleCollection styles = tableFlowLayoutPanelAnswer.ColumnStyles;
if (styles.Count == )
{
styles[].Width = AnswerWidth/ - ;
styles[].Width = AnswerWidth/ - ;
styles[].Width = ;
styles[].Width = ;
styles[].Width = ;
}
tableFlowLayoutPanelAnswer.Controls.Clear();
tableFlowLayoutPanelAnswer.Controls.Add(NumberA, , );
tableFlowLayoutPanelAnswer.Controls.Add(NumberB, , ); TableLayoutColumnStyleCollection styles2 = tableLayoutPanel1.ColumnStyles;
styles2[].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 > && NumberBInput >= && NumberAInput - NumberBInput >= )
{
NumberAInput = tmp;
Score = Math.Round((NumberAInput - NumberBInput) / NumberAInput, ) * AssignedValue;
lbScore.Text = Score.ToString();
errorProvider.SetError(NumberA, "");
}
else
{
lbScore.Text = "";
errorProvider.SetError(NumberA, "该行数据有错");
} }
else
{
NumberAInput = ;
lbScore.Text = "";
errorProvider.SetError(NumberA, "该值不是一个数据。");
} if (Score == )
{
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 > && NumberBInput >= && NumberAInput - NumberBInput >= )
{
NumberBInput = tmp;
Score = Math.Round((NumberAInput - NumberBInput) / NumberAInput, ) * AssignedValue;
lbScore.Text = Score.ToString();
errorProvider.SetError(NumberB, "");
}
else
{
errorProvider.SetError(NumberB, "该行数据有错");
lbScore.Text = "";
}
}
else
{
NumberBInput = ;
lbScore.Text = "";
MessageBox.Show("该值不是一个数据。");
} if (Score == )
{
tableLayoutPanel1.BackColor = Color.LightPink;
}
else
{
tableLayoutPanel1.BackColor = Color.LawnGreen;
} Clicked(sender, e);
} }
}