首页 > 代码库 > RadioButton Control
RadioButton Control
RadiobuttonFive usage:
DataTable table = new DataTable(); table.TableName = "Parent"; table.Columns.Add("Index_Code", typeof(string)); table.Columns.Add("Index_Item_Name", typeof(string)); table.Columns.Add("Index_Item_Value", typeof(decimal)); table.Columns.Add("Score", typeof(decimal)); table.Rows.Add("11", "How are you?", 10, 0); table.Rows.Add("11", "What is your favor?", 10, 0); FlowLayoutPanel flp = new FlowLayoutPanel(); this.Controls.Add(flp); flp.Width = 800; int c = 1; foreach (DataRow item in table.Rows) { decimal d1 = (decimal)item["Index_Item_Value"]; decimal d2 = (decimal)item["Score"]; RadioButtonFive rb = new RadioButtonFive(c, item["Index_Code"].ToString(), item["Index_Item_Name"].ToString(), d1, d2, 200); rb.AnswerTypes = 3; rb.Col2Width = 300; rb.AllowDataChanged = true; rb.Refresh(); flp.Controls.Add(rb); rb.ProperAnswerType(); }
RadioButtonFive user control
Code
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 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; 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 =http://www.mamicode.com/ aValue; this.Score = aScore; lbSerialNo.Text = SerialNo.ToString(); lbQuestion.Text = Question; lbValue.Text = AssignedValue.ToString(); lbScore.Text = Score.ToString(); Col2Width = questionWidth; 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 =http://www.mamicode.com/ aValue; this.Score = aScore; lbSerialNo.Text = SerialNo.ToString(); lbQuestion.Text = Question; lbValue.Text = AssignedValue.ToString(); lbScore.Text = Score.ToString(); Col2Width = questionWidth; errorProvider = new ErrorProvider(); 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(); NumberB.Text = NumberBInput.ToString(); if (AllowDataChanged == false) { NumberA.Enabled = false; NumberB.Enabled = false; } } } 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 =http://www.mamicode.com/= 0) { tableLayoutPanel1.BackColor = Color.LightPink; } else { tableLayoutPanel1.BackColor = Color.LawnGreen; } Clicked(sender, e); } private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e) { } private void RadioButtonFive_Load(object sender, EventArgs 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 = "Choosed"; rbB.Text = "Unchoosed"; TableLayoutColumnStyleCollection styles = tableFlowLayoutPanelAnswer.ColumnStyles; if (styles.Count == 5) { styles[0].Width = 84; styles[1].Width = 84; styles[2].Width = 0; styles[3].Width = 0; styles[4].Width = 0; } TableLayoutColumnStyleCollection styles2 = tableLayoutPanel1.ColumnStyles; styles2[3].Width = 170; } 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.Validated += NumberA_Validated; NumberB.Validated += NumberB_Validated; NumberA.Width = 60; NumberB.Width = 60; TableLayoutColumnStyleCollection styles = tableFlowLayoutPanelAnswer.ColumnStyles; if (styles.Count == 5) { styles[0].Width = 84; styles[1].Width = 84; styles[2].Width = 0; styles[3].Width = 0; styles[4].Width = 0; } tableFlowLayoutPanelAnswer.Controls.Clear(); tableFlowLayoutPanelAnswer.Controls.Add(NumberA, 0, 0); //NumberB.Dock = DockStyle.Right; tableFlowLayoutPanelAnswer.Controls.Add(NumberB, 1, 0); TableLayoutColumnStyleCollection styles2 = tableLayoutPanel1.ColumnStyles; styles2[3].Width = 170; NumberB.Invalidate(); } } void NumberA_Validated(object sender, EventArgs e) { decimal tmp; if (decimal.TryParse(NumberA.Text.ToString(), out tmp)) { if (tmp < 0 || tmp > 100) { NumberAInput = tmp; errorProvider.SetError(NumberA, "number is out of range"); } else { NumberAInput = tmp; errorProvider.SetError(NumberA, ""); } } else { NumberAInput = 0; errorProvider.SetError(NumberA, "Not a number"); } } void NumberB_Validated(object sender, EventArgs e) { decimal tmp; if (decimal.TryParse(NumberB.Text.ToString(), out tmp)) { if (tmp < 0 || tmp > 100) { NumberBInput = tmp; errorProvider.SetError(NumberB, "number is out of range"); } else { NumberBInput = tmp; errorProvider.SetError(NumberB, ""); } } else { NumberBInput = 0; errorProvider.SetError(NumberB, "Not a number"); } } }}
UI
namespace WFControlLibrary{ partial class RadioButtonFive { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.rbA = new System.Windows.Forms.RadioButton(); this.rbB = new System.Windows.Forms.RadioButton(); this.rbC = new System.Windows.Forms.RadioButton(); this.rbD = new System.Windows.Forms.RadioButton(); this.rbE = new System.Windows.Forms.RadioButton(); this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.lbScore = new System.Windows.Forms.Label(); this.lbValue = http://www.mamicode.com/new System.Windows.Forms.Label(); this.lbQuestion = new System.Windows.Forms.Label(); this.tableFlowLayoutPanelAnswer = new System.Windows.Forms.TableLayoutPanel(); this.lbSerialNo = new System.Windows.Forms.Label(); this.tableLayoutPanel1.SuspendLayout(); this.tableFlowLayoutPanelAnswer.SuspendLayout(); this.SuspendLayout(); // // rbA // this.rbA.AutoSize = true; this.rbA.Location = new System.Drawing.Point(3, 3); this.rbA.Name = "rbA"; this.rbA.Size = new System.Drawing.Size(32, 17); this.rbA.TabIndex = 0; this.rbA.Text = "A"; this.rbA.UseVisualStyleBackColor = true; this.rbA.CheckedChanged += new System.EventHandler(this.rbA_CheckedChanged); // // rbB // this.rbB.AutoSize = true; this.rbB.Location = new System.Drawing.Point(43, 3); this.rbB.Name = "rbB"; this.rbB.Size = new System.Drawing.Size(32, 17); this.rbB.TabIndex = 1; this.rbB.Text = "B"; this.rbB.UseVisualStyleBackColor = true; this.rbB.CheckedChanged += new System.EventHandler(this.rbA_CheckedChanged); // // rbC // this.rbC.AutoSize = true; this.rbC.Location = new System.Drawing.Point(83, 3); this.rbC.Name = "rbC"; this.rbC.Size = new System.Drawing.Size(32, 17); this.rbC.TabIndex = 2; this.rbC.Text = "C"; this.rbC.UseVisualStyleBackColor = true; this.rbC.CheckedChanged += new System.EventHandler(this.rbA_CheckedChanged); // // rbD // this.rbD.AutoSize = true; this.rbD.Location = new System.Drawing.Point(123, 3); this.rbD.Name = "rbD"; this.rbD.Size = new System.Drawing.Size(33, 17); this.rbD.TabIndex = 3; this.rbD.Text = "D"; this.rbD.UseVisualStyleBackColor = true; this.rbD.CheckedChanged += new System.EventHandler(this.rbA_CheckedChanged); // // rbE // this.rbE.AutoSize = true; this.rbE.Checked = true; this.rbE.Location = new System.Drawing.Point(163, 3); this.rbE.Name = "rbE"; this.rbE.Size = new System.Drawing.Size(61, 17); this.rbE.TabIndex = 4; this.rbE.TabStop = true; this.rbE.Text = "未回答"; this.rbE.UseVisualStyleBackColor = true; this.rbE.CheckedChanged += new System.EventHandler(this.rbA_CheckedChanged); // // tableLayoutPanel1 // this.tableLayoutPanel1.AutoSize = true; this.tableLayoutPanel1.BackColor = System.Drawing.SystemColors.InactiveCaption; this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single; this.tableLayoutPanel1.ColumnCount = 5; this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 40F)); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 60F)); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 229F)); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 90F)); this.tableLayoutPanel1.Controls.Add(this.lbScore, 4, 0); this.tableLayoutPanel1.Controls.Add(this.lbValue, 2, 0); this.tableLayoutPanel1.Controls.Add(this.lbQuestion, 1, 0); this.tableLayoutPanel1.Controls.Add(this.tableFlowLayoutPanelAnswer, 3, 0); this.tableLayoutPanel1.Controls.Add(this.lbSerialNo, 0, 0); this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Left; this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.RowCount = 1; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); this.tableLayoutPanel1.Size = new System.Drawing.Size(466, 44); this.tableLayoutPanel1.TabIndex = 5; this.tableLayoutPanel1.Paint += new System.Windows.Forms.PaintEventHandler(this.tableLayoutPanel1_Paint); // // lbScore // this.lbScore.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.lbScore.AutoSize = true; this.lbScore.Location = new System.Drawing.Point(378, 1); this.lbScore.Name = "lbScore"; this.lbScore.Size = new System.Drawing.Size(84, 42); this.lbScore.TabIndex = 9; this.lbScore.Text = "label4"; this.lbScore.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbValue // this.lbValue.Dock = System.Windows.Forms.DockStyle.Fill; this.lbValue.Location = new System.Drawing.Point(87, 1); this.lbValue.Name = "lbValue"; this.lbValue.Size = new System.Drawing.Size(54, 42); this.lbValue.TabIndex = 8; this.lbValue.Text = "label3"; this.lbValue.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // lbQuestion // this.lbQuestion.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.lbQuestion.AutoSize = true; this.lbQuestion.Location = new System.Drawing.Point(45, 1); this.lbQuestion.Name = "lbQuestion"; this.lbQuestion.Size = new System.Drawing.Size(35, 42); this.lbQuestion.TabIndex = 7; this.lbQuestion.Text = "label2"; this.lbQuestion.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // tableFlowLayoutPanelAnswer // this.tableFlowLayoutPanelAnswer.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.tableFlowLayoutPanelAnswer.ColumnCount = 5; this.tableFlowLayoutPanelAnswer.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 40F)); this.tableFlowLayoutPanelAnswer.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 40F)); this.tableFlowLayoutPanelAnswer.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 40F)); this.tableFlowLayoutPanelAnswer.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 40F)); this.tableFlowLayoutPanelAnswer.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableFlowLayoutPanelAnswer.Controls.Add(this.rbE, 4, 0); this.tableFlowLayoutPanelAnswer.Controls.Add(this.rbC, 2, 0); this.tableFlowLayoutPanelAnswer.Controls.Add(this.rbD, 3, 0); this.tableFlowLayoutPanelAnswer.Controls.Add(this.rbA, 0, 0); this.tableFlowLayoutPanelAnswer.Controls.Add(this.rbB, 1, 0); this.tableFlowLayoutPanelAnswer.Location = new System.Drawing.Point(148, 4); this.tableFlowLayoutPanelAnswer.Name = "tableFlowLayoutPanelAnswer"; this.tableFlowLayoutPanelAnswer.RowCount = 1; this.tableFlowLayoutPanelAnswer.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); this.tableFlowLayoutPanelAnswer.Size = new System.Drawing.Size(223, 36); this.tableFlowLayoutPanelAnswer.TabIndex = 5; // // lbSerialNo // this.lbSerialNo.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.lbSerialNo.Location = new System.Drawing.Point(4, 1); this.lbSerialNo.Name = "lbSerialNo"; this.lbSerialNo.Size = new System.Drawing.Size(34, 42); this.lbSerialNo.TabIndex = 6; this.lbSerialNo.Text = "lb1"; this.lbSerialNo.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // RadioButtonFive // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.tableLayoutPanel1); this.Margin = new System.Windows.Forms.Padding(0); this.Name = "RadioButtonFive"; this.Size = new System.Drawing.Size(1077, 44); this.Load += new System.EventHandler(this.RadioButtonFive_Load); this.tableLayoutPanel1.ResumeLayout(false); this.tableLayoutPanel1.PerformLayout(); this.tableFlowLayoutPanelAnswer.ResumeLayout(false); this.tableFlowLayoutPanelAnswer.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.RadioButton rbA; private System.Windows.Forms.RadioButton rbB; private System.Windows.Forms.RadioButton rbC; private System.Windows.Forms.RadioButton rbD; private System.Windows.Forms.RadioButton rbE; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; private System.Windows.Forms.Label lbScore; private System.Windows.Forms.Label lbValue; private System.Windows.Forms.Label lbQuestion; private System.Windows.Forms.TableLayoutPanel tableFlowLayoutPanelAnswer; private System.Windows.Forms.Label lbSerialNo; }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。