Csharp/C Sharp/GUI Windows Form/RadioButton — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:33, 26 мая 2010
Содержание
Get selected radio button
<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.CheckBox checkBox1; private System.Windows.Forms.CheckBox checkBox2; private System.Windows.Forms.CheckBox checkBox3; private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.RadioButton radioButton3; private System.Windows.Forms.Button button1; private System.ruponentModel.Container components = null; public Form1() { InitializeComponent(); } private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.checkBox1 = new System.Windows.Forms.CheckBox(); this.checkBox2 = new System.Windows.Forms.CheckBox(); this.checkBox3 = new System.Windows.Forms.CheckBox(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.radioButton3 = new System.Windows.Forms.RadioButton(); this.button1 = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.radioButton1, this.radioButton2, this.radioButton3}); this.groupBox1.Location = new System.Drawing.Point(8, 120); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(120, 144); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Color"; this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter); // // checkBox1 // this.checkBox1.Location = new System.Drawing.Point(8, 8); this.checkBox1.Name = "checkBox1"; this.checkBox1.TabIndex = 1; this.checkBox1.Text = "Circle"; // // checkBox2 // this.checkBox2.Location = new System.Drawing.Point(8, 40); this.checkBox2.Name = "checkBox2"; this.checkBox2.TabIndex = 2; this.checkBox2.Text = "Rectangle"; // // checkBox3 // this.checkBox3.Location = new System.Drawing.Point(8, 72); this.checkBox3.Name = "checkBox3"; this.checkBox3.TabIndex = 3; this.checkBox3.Text = "String"; // // radioButton1 // this.radioButton1.Location = new System.Drawing.Point(8, 32); this.radioButton1.Name = "radioButton1"; this.radioButton1.TabIndex = 4; this.radioButton1.Text = "Red"; this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); // // radioButton2 // this.radioButton2.Location = new System.Drawing.Point(8, 64); this.radioButton2.Name = "radioButton2"; this.radioButton2.TabIndex = 5; this.radioButton2.Text = "Green"; // // radioButton3 // this.radioButton3.Location = new System.Drawing.Point(8, 96); this.radioButton3.Name = "radioButton3"; this.radioButton3.TabIndex = 6; this.radioButton3.Text = "Blue"; // // button1 // this.button1.Location = new System.Drawing.Point(8, 280); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(112, 32); this.button1.TabIndex = 4; this.button1.Text = "Draw"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(408, 317); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1, this.checkBox3, this.checkBox2, this.checkBox1, this.groupBox1}); this.Name = "Form1"; this.Text = "CheckBox and RadioButton Sample"; this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new Form1()); } private void groupBox1_Enter(object sender, System.EventArgs e) { Console.WriteLine("group box enter event"); } private void radioButton1_CheckedChanged(object sender, System.EventArgs e) { Console.WriteLine("Radio Button checked changed event"); } private void button1_Click(object sender, System.EventArgs e) { Graphics g = Graphics.FromHwnd(this.Handle); String str = ""; Rectangle rc = new Rectangle(150, 50, 250, 250); if(radioButton1.Checked) { str = "red"; } if(radioButton2.Checked) { str+="Green"; } if(radioButton3.Checked) { str+="Blue"; } if (checkBox1.Checked) { str+="Ellipse"; } if (checkBox2.Checked) { str += "Rectangle"; } if (checkBox3.Checked) { g.FillRectangle(new SolidBrush(Color.White), rc); g.DrawString(str, new Font("Verdana", 12), new SolidBrush(Color.Black), rc); } } }
</source>
Load image to RadioButton
<source lang="csharp"> using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form {
private System.Windows.Forms.RadioButton radioButton1; public Form1() { InitializeComponent(); } private void InitializeComponent() { this.radioButton1 = new System.Windows.Forms.RadioButton(); this.SuspendLayout(); this.radioButton1.Image = new Bitmap("winter.jpg"); this.radioButton1.Location = new System.Drawing.Point(12, 195); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(224, 47); this.radioButton1.TabIndex = 3; this.radioButton1.TabStop = true; this.radioButton1.Text = "radioButton1"; this.radioButton1.UseVisualStyleBackColor = true; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(299, 271); this.Controls.Add(this.radioButton1); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Name = "ImagesInCommonControls"; this.Text = "ImagesInCommonControls"; this.ResumeLayout(false); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }
}
</source>
Radio button check changed event
<source lang="csharp"> using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.CheckBox checkBox1; private System.Windows.Forms.CheckBox checkBox2; private System.Windows.Forms.CheckBox checkBox3; private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.RadioButton radioButton3; private System.Windows.Forms.Button button1; private System.ruponentModel.Container components = null; public Form1() { InitializeComponent(); } private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.checkBox1 = new System.Windows.Forms.CheckBox(); this.checkBox2 = new System.Windows.Forms.CheckBox(); this.checkBox3 = new System.Windows.Forms.CheckBox(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.radioButton3 = new System.Windows.Forms.RadioButton(); this.button1 = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.radioButton1, this.radioButton2, this.radioButton3}); this.groupBox1.Location = new System.Drawing.Point(8, 120); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(120, 144); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Color"; this.groupBox1.Enter += new System.EventHandler(this.groupBox1_Enter); // // checkBox1 // this.checkBox1.Location = new System.Drawing.Point(8, 8); this.checkBox1.Name = "checkBox1"; this.checkBox1.TabIndex = 1; this.checkBox1.Text = "Circle"; // // checkBox2 // this.checkBox2.Location = new System.Drawing.Point(8, 40); this.checkBox2.Name = "checkBox2"; this.checkBox2.TabIndex = 2; this.checkBox2.Text = "Rectangle"; // // checkBox3 // this.checkBox3.Location = new System.Drawing.Point(8, 72); this.checkBox3.Name = "checkBox3"; this.checkBox3.TabIndex = 3; this.checkBox3.Text = "String"; // // radioButton1 // this.radioButton1.Location = new System.Drawing.Point(8, 32); this.radioButton1.Name = "radioButton1"; this.radioButton1.TabIndex = 4; this.radioButton1.Text = "Red"; this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); // // radioButton2 // this.radioButton2.Location = new System.Drawing.Point(8, 64); this.radioButton2.Name = "radioButton2"; this.radioButton2.TabIndex = 5; this.radioButton2.Text = "Green"; // // radioButton3 // this.radioButton3.Location = new System.Drawing.Point(8, 96); this.radioButton3.Name = "radioButton3"; this.radioButton3.TabIndex = 6; this.radioButton3.Text = "Blue"; // // button1 // this.button1.Location = new System.Drawing.Point(8, 280); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(112, 32); this.button1.TabIndex = 4; this.button1.Text = "Draw"; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(408, 317); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1, this.checkBox3, this.checkBox2, this.checkBox1, this.groupBox1}); this.Name = "Form1"; this.Text = "CheckBox and RadioButton Sample"; this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new Form1()); } private void groupBox1_Enter(object sender, System.EventArgs e) { Console.WriteLine("group box enter event"); } private void radioButton1_CheckedChanged(object sender, System.EventArgs e) { Console.WriteLine("Radio Button checked changed event"); } private void button1_Click(object sender, System.EventArgs e) { Graphics g = Graphics.FromHwnd(this.Handle); String str = ""; Rectangle rc = new Rectangle(150, 50, 250, 250); if(radioButton1.Checked) { str = "red"; } if(radioButton2.Checked) { str+="Green"; } if(radioButton3.Checked) { str+="Blue"; } if (checkBox1.Checked) { str+="Ellipse"; } if (checkBox2.Checked) { str += "Rectangle"; } if (checkBox3.Checked) { g.FillRectangle(new SolidBrush(Color.White), rc); g.DrawString(str, new Font("Verdana", 12), new SolidBrush(Color.Black), rc); } } }
</source>
RadioButton check state change event
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms;
public class Test : Form {
private RadioButton square = new RadioButton(); private RadioButton circle = new RadioButton(); private ComboBox color = new ComboBox(); private Color c = Color.Red; public Test( ) { Text = "Select Item"; square.Text = "Square"; circle.Text = "Circle"; color.Text = "Choose a color"; Size = new Size(400,250); int w = 20; square.Location = new Point(w, 30); circle.Location = new Point(w += 10 + square.Width, 30); color.Location = new Point(w += 10 + circle.Width, 30); color.Items.Add("Red"); color.Items.Add("Green"); color.Items.Add("Blue"); Controls.Add(square); Controls.Add(circle); Controls.Add(color); square.CheckedChanged += new EventHandler(Checked_Changed); circle.CheckedChanged += new EventHandler(Checked_Changed); color.SelectedIndexChanged += new EventHandler(Selected_Index); } protected override void OnPaint(PaintEventArgs e){ Graphics g = e.Graphics; Brush brush = new SolidBrush(c); if (square.Checked) g.FillRectangle(brush,100,100,100,100); else g.FillEllipse(brush,100,100,100,100); base.OnPaint( e ); } protected void Selected_Index(Object sender, EventArgs e){ if (color.SelectedItem.ToString() == "Red" ) c = Color.Red; else if (color.SelectedItem.ToString() == "Green") c = Color.Green; else c = Color.Blue; Invalidate(); } protected void Checked_Changed(Object sender, EventArgs e) { Invalidate(); } static void Main() { Application.Run(new Test()); }
}
</source>
Radio Button click event
<source lang="csharp"> using System; using System.Drawing; using System.Windows.Forms; public class SelectItem : Form {
private RadioButton square = new RadioButton(); private RadioButton circle = new RadioButton(); private ComboBox color = new ComboBox(); public SelectItem( ) { Text = "Select Item"; square.Text = "Square"; circle.Text = "Circle"; color.Text = "Choose a color"; Size = new Size(400,250); int w = 20; square.Location = new Point(w, 30); circle.Location = new Point(w += 10 + square.Width, 30); color.Location = new Point(w += 10 + circle.Width, 30); color.Items.Add("Red"); color.Items.Add("Green"); color.Items.Add("Blue"); Controls.Add(square); Controls.Add(circle); Controls.Add(color); square.CheckedChanged += new EventHandler(Checked_Changed); circle.CheckedChanged += new EventHandler(Checked_Changed); color.SelectedIndexChanged += new EventHandler(Selected_Index); } protected void Selected_Index(Object sender, EventArgs e) { if (color.SelectedItem.ToString() == "Red" ) Console.WriteLine("It is red."); else if (color.SelectedItem.ToString() == "Green") Console.WriteLine("It is green."); else Console.WriteLine("It is Blue"); } protected void Checked_Changed(Object sender, EventArgs e) { if (square.Checked) Console.WriteLine("It is rectangle"); else Console.WriteLine("Ellipse"); } static void Main() { Application.Run(new SelectItem()); }
}
</source>
RadioButton on a form
<source lang="csharp"> /* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace Radio {
/// <summary> /// Summary description for FormRadio. /// </summary> public class FormRadio : System.Windows.Forms.Form { private System.Windows.Forms.Panel panel1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton3; private System.Windows.Forms.RadioButton radioButton5; private System.Windows.Forms.RadioButton radioButton4; private System.Windows.Forms.Panel panel2; private System.Windows.Forms.RadioButton radioButton6; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public FormRadio() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.panel1 = new System.Windows.Forms.Panel(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.radioButton3 = new System.Windows.Forms.RadioButton(); this.radioButton5 = new System.Windows.Forms.RadioButton(); this.radioButton4 = new System.Windows.Forms.RadioButton(); this.panel2 = new System.Windows.Forms.Panel(); this.radioButton6 = new System.Windows.Forms.RadioButton(); this.panel1.SuspendLayout(); this.panel2.SuspendLayout(); this.SuspendLayout(); // // panel1 // this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] { this.radioButton2, this.radioButton1, this.radioButton3}); this.panel1.Location = new System.Drawing.Point(8, 16); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(136, 136); this.panel1.TabIndex = 1; // // radioButton2 // this.radioButton2.Location = new System.Drawing.Point(16, 56); this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(96, 16); this.radioButton2.TabIndex = 0; this.radioButton2.Text = "radioButton2"; // // radioButton1 // this.radioButton1.Location = new System.Drawing.Point(16, 16); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(96, 16); this.radioButton1.TabIndex = 0; this.radioButton1.Text = "radioButton1"; // // radioButton3 // this.radioButton3.Location = new System.Drawing.Point(16, 96); this.radioButton3.Name = "radioButton3"; this.radioButton3.Size = new System.Drawing.Size(96, 16); this.radioButton3.TabIndex = 0; this.radioButton3.Text = "radioButton3"; // // radioButton5 // this.radioButton5.Location = new System.Drawing.Point(16, 56); this.radioButton5.Name = "radioButton5"; this.radioButton5.Size = new System.Drawing.Size(96, 16); this.radioButton5.TabIndex = 0; this.radioButton5.Text = "radioButton5"; // // radioButton4 // this.radioButton4.Location = new System.Drawing.Point(16, 16); this.radioButton4.Name = "radioButton4"; this.radioButton4.Size = new System.Drawing.Size(96, 16); this.radioButton4.TabIndex = 0; this.radioButton4.Text = "radioButton4"; // // panel2 // this.panel2.Controls.AddRange(new System.Windows.Forms.Control[] { this.radioButton6, this.radioButton4, this.radioButton5}); this.panel2.Location = new System.Drawing.Point(152, 16); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(136, 136); this.panel2.TabIndex = 2; // // radioButton6 // this.radioButton6.Location = new System.Drawing.Point(16, 96); this.radioButton6.Name = "radioButton6"; this.radioButton6.Size = new System.Drawing.Size(96, 16); this.radioButton6.TabIndex = 0; this.radioButton6.Text = "radioButton6"; // // FormRadio // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.panel1, this.panel2}); this.Name = "FormRadio"; this.Text = "FormRadio"; this.panel1.ResumeLayout(false); this.panel2.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new FormRadio()); } }
}
</source>
RadioButton With Img
<source lang="csharp"> /* Professional Windows GUI Programming Using C# by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information ISBN: 1861007663
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace RadioButtonWithImg {
/// <summary> /// Summary description for RadioButtonWithImg. /// </summary> public class RadioButtonWithImg : System.Windows.Forms.Form { private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.RadioButton radioButton3; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public RadioButtonWithImg() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.radioButton3 = new System.Windows.Forms.RadioButton(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.radioButton3, this.radioButton2, this.radioButton1}); this.groupBox1.Location = new System.Drawing.Point(8, 16); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(160, 120); this.groupBox1.TabIndex = 1; this.groupBox1.TabStop = false; this.groupBox1.Text = "Group 1"; // // radioButton3 // this.radioButton3.Appearance = System.Windows.Forms.Appearance.Button; this.radioButton3.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(255)), ((System.Byte)(255))); this.radioButton3.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.radioButton3.Location = new System.Drawing.Point(16, 88); this.radioButton3.Name = "radioButton3"; this.radioButton3.Size = new System.Drawing.Size(120, 24); this.radioButton3.TabIndex = 2; this.radioButton3.Text = "Option3"; // // radioButton2 // this.radioButton2.Appearance = System.Windows.Forms.Appearance.Button; this.radioButton2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192))); this.radioButton2.Location = new System.Drawing.Point(16, 56); this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(120, 24); this.radioButton2.TabIndex = 1; this.radioButton2.Text = "Option2"; this.radioButton2.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // radioButton1 // this.radioButton1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(192)), ((System.Byte)(192))); this.radioButton1.Location = new System.Drawing.Point(16, 24); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(120, 24); this.radioButton1.TabIndex = 0; this.radioButton1.Text = "Option1"; this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); // // RadioButton // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(184, 149); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.groupBox1}); this.Name = "RadioButton"; this.Text = "Radio Button"; this.Load += new System.EventHandler(this.RadioButtonWithImg_Load); this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new RadioButtonWithImg()); } private void RadioButtonWithImg_Load(object sender, System.EventArgs e) { // Select the Image for the button Image img = Image.FromFile("EYE.ICO"); // Assign the Image to the button radioButton1.Image = img ; // Align the image radioButton1.ImageAlign = ContentAlignment.MiddleRight; // Select the Image for the button img = Image.FromFile("WRENCH.ICO"); // Assign the Image to the button radioButton2.Image = img ; // Align the image radioButton2.ImageAlign = ContentAlignment.MiddleLeft; } private void radioButton1_CheckedChanged(object sender, System.EventArgs e) { if (radioButton1.Checked) MessageBox.Show("Checked!"); else MessageBox.Show("Not checked!"); } }
}
</source>
<A href="http://www.nfex.ru/Code/CSharpDownload/RadioButton.zip">RadioButton.zip( 30 k)</a>
Radio Group
<source lang="csharp"> /* Professional Windows GUI Programming Using C# by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information ISBN: 1861007663
- /
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; namespace Peaks {
/// <summary> /// Summary description for Peaks. /// </summary> public class Peaks : System.Windows.Forms.Form { private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.RadioButton radioButton3; private System.Windows.Forms.RadioButton radioButton4; private System.Windows.Forms.RadioButton radioButton5; private System.Windows.Forms.RadioButton radioButton6; private System.Windows.Forms.RadioButton radioButton7; private System.Windows.Forms.RadioButton radioButton8; private System.Windows.Forms.RadioButton radioButton9; private System.Windows.Forms.RadioButton radioButton10; private System.Windows.Forms.RadioButton radioButton11; private System.Windows.Forms.RadioButton radioButton12; Point[] pnts = { new Point(20, 90), new Point(55, 70), new Point(80, 80), new Point(105, 40), new Point(130, 90)}; /// <summary> /// Required designer variable. /// </summary> private System.ruponentModel.Container components = null; public Peaks() { // // Required for Windows Form Designer support // InitializeComponent(); this.radioButton1.CheckedChanged += new System.EventHandler(RBs_CheckedChanged); this.radioButton2.CheckedChanged += new System.EventHandler(RBs_CheckedChanged); this.radioButton3.CheckedChanged += new System.EventHandler(RBs_CheckedChanged); this.radioButton4.CheckedChanged += new System.EventHandler(RBs_CheckedChanged); this.radioButton5.CheckedChanged += new System.EventHandler(RBs_CheckedChanged); this.radioButton6.CheckedChanged += new System.EventHandler(RBs_CheckedChanged); this.radioButton7.CheckedChanged += new System.EventHandler(RBs_CheckedChanged); this.radioButton8.CheckedChanged += new System.EventHandler(RBs_CheckedChanged); this.radioButton9.CheckedChanged += new System.EventHandler(RBs_CheckedChanged); this.radioButton10.CheckedChanged += new System.EventHandler(RBs_CheckedChanged); this.radioButton11.CheckedChanged += new System.EventHandler(RBs_CheckedChanged); this.radioButton12.CheckedChanged += new System.EventHandler(RBs_CheckedChanged); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.groupBox1 = new System.Windows.Forms.GroupBox(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.radioButton3 = new System.Windows.Forms.RadioButton(); this.radioButton6 = new System.Windows.Forms.RadioButton(); this.radioButton5 = new System.Windows.Forms.RadioButton(); this.radioButton4 = new System.Windows.Forms.RadioButton(); this.radioButton7 = new System.Windows.Forms.RadioButton(); this.radioButton9 = new System.Windows.Forms.RadioButton(); this.radioButton8 = new System.Windows.Forms.RadioButton(); this.radioButton10 = new System.Windows.Forms.RadioButton(); this.radioButton12 = new System.Windows.Forms.RadioButton(); this.radioButton11 = new System.Windows.Forms.RadioButton(); this.groupBox1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.radioButton1, this.radioButton2, this.radioButton3, this.radioButton6, this.radioButton5, this.radioButton4, this.radioButton7, this.radioButton9, this.radioButton8, this.radioButton10, this.radioButton12, this.radioButton11}); this.groupBox1.Location = new System.Drawing.Point(200, 8); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(304, 120); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Primitives"; // // radioButton1 // this.radioButton1.Location = new System.Drawing.Point(16, 24); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(88, 16); this.radioButton1.TabIndex = 0; this.radioButton1.Text = "4xLine"; // // radioButton2 // this.radioButton2.Location = new System.Drawing.Point(112, 24); this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(56, 16); this.radioButton2.TabIndex = 0; this.radioButton2.Text = "Lines"; // // radioButton3 // this.radioButton3.Location = new System.Drawing.Point(184, 24); this.radioButton3.Name = "radioButton3"; this.radioButton3.Size = new System.Drawing.Size(80, 16); this.radioButton3.TabIndex = 0; this.radioButton3.Text = "Polygon"; // // radioButton6 // this.radioButton6.Location = new System.Drawing.Point(184, 48); this.radioButton6.Name = "radioButton6"; this.radioButton6.Size = new System.Drawing.Size(80, 16); this.radioButton6.TabIndex = 0; this.radioButton6.Text = "Curve 1.1"; // // radioButton5 // this.radioButton5.Location = new System.Drawing.Point(112, 48); this.radioButton5.Name = "radioButton5"; this.radioButton5.Size = new System.Drawing.Size(88, 16); this.radioButton5.TabIndex = 0; this.radioButton5.Text = "Curve 0.6"; // // radioButton4 // this.radioButton4.Location = new System.Drawing.Point(16, 48); this.radioButton4.Name = "radioButton4"; this.radioButton4.Size = new System.Drawing.Size(88, 16); this.radioButton4.TabIndex = 0; this.radioButton4.Text = "Curve 0"; // // radioButton7 // this.radioButton7.Location = new System.Drawing.Point(16, 72); this.radioButton7.Name = "radioButton7"; this.radioButton7.Size = new System.Drawing.Size(88, 16); this.radioButton7.TabIndex = 0; this.radioButton7.Text = "2xBezier"; // // radioButton9 // this.radioButton9.Location = new System.Drawing.Point(184, 72); this.radioButton9.Name = "radioButton9"; this.radioButton9.Size = new System.Drawing.Size(112, 16); this.radioButton9.TabIndex = 0; this.radioButton9.Text = "ClosedCurve 1.1"; // // radioButton8 // this.radioButton8.Location = new System.Drawing.Point(112, 72); this.radioButton8.Name = "radioButton8"; this.radioButton8.Size = new System.Drawing.Size(88, 16); this.radioButton8.TabIndex = 0; this.radioButton8.Text = "Beziers"; // // radioButton10 // this.radioButton10.Location = new System.Drawing.Point(16, 96); this.radioButton10.Name = "radioButton10"; this.radioButton10.Size = new System.Drawing.Size(88, 16); this.radioButton10.TabIndex = 0; this.radioButton10.Text = "2xBezier"; // // radioButton12 // this.radioButton12.Location = new System.Drawing.Point(184, 96); this.radioButton12.Name = "radioButton12"; this.radioButton12.Size = new System.Drawing.Size(88, 16); this.radioButton12.TabIndex = 0; this.radioButton12.Text = "Curve"; // // radioButton11 // this.radioButton11.Location = new System.Drawing.Point(112, 96); this.radioButton11.Name = "radioButton11"; this.radioButton11.Size = new System.Drawing.Size(88, 16); this.radioButton11.TabIndex = 0; this.radioButton11.Text = "Beziers"; // // Peaks // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(504, 133); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.groupBox1}); this.Name = "Peaks"; this.Text = "Two Peaks"; this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Peaks()); } private void RBs_CheckedChanged(object sender, System.EventArgs e) { this.Refresh(); } protected override void OnPaint (System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; g.Clear(this.BackColor); if (this.radioButton1.Checked) Ex01_4xLine(g); if (this.radioButton2.Checked) Ex02_Lines(g); if (this.radioButton3.Checked) Ex03_Polygon(g); if (this.radioButton4.Checked) Ex04_Curve0(g); if (this.radioButton5.Checked) Ex05_Curve1(g); if (this.radioButton6.Checked) Ex06_Curve2(g); if (this.radioButton7.Checked) Ex07_2xBezier(g); if (this.radioButton8.Checked) Ex08_Beziers(g); if (this.radioButton9.Checked) Ex09_ClosedCurve(g); if (this.radioButton10.Checked) Ex10_2xBezier_2(g); if (this.radioButton11.Checked) Ex11_Beziers_2(g); if (this.radioButton12.Checked) Ex11_Curve_2(g); g.Dispose(); } protected void Ex01_4xLine(Graphics g) { Pen pn = new Pen(Color.Blue, 2); g.DrawLine(pn, 20, 90, 55, 70); g.DrawLine(pn, 55, 70, 80, 80); g.DrawLine(pn, 80, 80,105, 40); g.DrawLine(pn,105, 40,130, 90); } protected void Ex02_Lines(Graphics g) { Pen pn = new Pen(Color.Blue, 2); g.DrawLines(pn, pnts); } protected void Ex03_Polygon(Graphics g) { Pen pn = new Pen(Color.Blue, 2); g.DrawPolygon(pn, pnts); } protected void Ex04_Curve0(Graphics g) { Pen pn = new Pen(Color.Blue, 2); g.DrawCurve(pn, pnts, 0.0f); } protected void Ex05_Curve1(Graphics g) { Pen pn = new Pen(Color.Blue, 2); g.DrawCurve(pn, pnts, 0.6f); } protected void Ex06_Curve2(Graphics g) { Pen pn = new Pen(Color.Blue, 2); g.DrawCurve(pn, pnts, 1.1f); } protected void Ex07_2xBezier(Graphics g) { Pen pn = new Pen(Color.Blue, 2); g.DrawBezier(pn, 20, 90, 50, 70, 60, 70, 80, 80); g.DrawBezier(pn, 80, 80,100, 40,110, 40,130, 90); } protected void Ex08_Beziers(Graphics g) { Pen pn = new Pen(Color.Blue, 2); Point[] pnts = {new Point(20, 90), new Point(50, 70), new Point(60, 70), new Point(80, 80), new Point(100, 40), new Point(110, 40), new Point(130, 90)}; g.DrawBeziers(pn, pnts); } protected void Ex09_ClosedCurve(Graphics g) { Pen pn = new Pen(Color.Blue, 2); Point[] pnts = {new Point( 20, 90), new Point( 55, 70), new Point( 80, 80), new Point(105, 40), new Point(130, 90)}; g.DrawClosedCurve(pn, pnts, 1.1f, System.Drawing.Drawing2D.FillMode.Winding); } protected void Ex10_2xBezier_2(Graphics g) { Pen pn = new Pen(Color.Blue, 2); g.DrawBezier(pn, 20, 90, 50, 70, 60, 70, 80, 80); g.DrawBezier(pn, 80, 80,100,120,110,120,130, 90); } protected void Ex11_Beziers_2(Graphics g) { Pen pn = new Pen(Color.Blue, 2); int[,] xy = { { 20, 90}, { 50, 70}, { 60, 70}, { 80, 80}, {100,120}, {110,120}, {130, 90}}; int ii = xy.GetLength(0); Point[] pnts = new Point[ii]; for(int i = 0; i < ii; i++) { pnts[i].X = xy[i, 0]; pnts[i].Y = xy[i, 1]; } g.DrawBeziers(pn, pnts); } protected void Ex11_Curve_2(Graphics g) { Pen pn = new Pen(Color.Blue, 2); Point[] pnts = {new Point( 20, 90), new Point( 55, 70), new Point( 80, 80), new Point(105,120), new Point(130, 90)}; g.DrawCurve(pn, pnts, 0.0f); } }
}
</source>
Using RadioButtons to set message window options
<source lang="csharp">
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class RadioButtonsTest : System.Windows.Forms.Form { private System.Windows.Forms.Label promptLabel; private System.Windows.Forms.Label displayLabel; private System.Windows.Forms.Button displayButton; private System.Windows.Forms.RadioButton questionButton; private System.Windows.Forms.RadioButton informationButton; private System.Windows.Forms.RadioButton exclamationButton; private System.Windows.Forms.RadioButton errorButton; private System.Windows.Forms.RadioButton retryCancelButton; private System.Windows.Forms.RadioButton yesNoButton; private System.Windows.Forms.RadioButton yesNoCancelButton; private System.Windows.Forms.RadioButton okCancelButton; private System.Windows.Forms.RadioButton okButton; private System.Windows.Forms.RadioButton abortRetryIgnoreButton; private System.Windows.Forms.GroupBox iconTypeGroupBox; private System.Windows.Forms.GroupBox buttonTypeGroupBox; private MessageBoxIcon iconType = MessageBoxIcon.Error; private MessageBoxButtons buttonType = MessageBoxButtons.OK; public RadioButtonsTest() { InitializeComponent(); } private void InitializeComponent() { this.informationButton = new System.Windows.Forms.RadioButton(); this.buttonTypeGroupBox = new System.Windows.Forms.GroupBox(); this.retryCancelButton = new System.Windows.Forms.RadioButton(); this.yesNoButton = new System.Windows.Forms.RadioButton(); this.yesNoCancelButton = new System.Windows.Forms.RadioButton(); this.abortRetryIgnoreButton = new System.Windows.Forms.RadioButton(); this.okCancelButton = new System.Windows.Forms.RadioButton(); this.okButton = new System.Windows.Forms.RadioButton(); this.iconTypeGroupBox = new System.Windows.Forms.GroupBox(); this.questionButton = new System.Windows.Forms.RadioButton(); this.exclamationButton = new System.Windows.Forms.RadioButton(); this.errorButton = new System.Windows.Forms.RadioButton(); this.displayLabel = new System.Windows.Forms.Label(); this.displayButton = new System.Windows.Forms.Button(); this.promptLabel = new System.Windows.Forms.Label(); this.buttonTypeGroupBox.SuspendLayout(); this.iconTypeGroupBox.SuspendLayout(); this.SuspendLayout(); // // informationButton // this.informationButton.Location = new System.Drawing.Point( 16, 104 ); this.informationButton.Name = "informationButton"; this.informationButton.Size = new System.Drawing.Size( 100, 23 ); this.informationButton.TabIndex = 4; this.informationButton.Text = "Information"; this.informationButton.CheckedChanged += new System.EventHandler(this.iconType_CheckedChanged ); // // buttonTypeGroupBox // this.buttonTypeGroupBox.Controls.AddRange(new System.Windows.Forms.Control[] { this.retryCancelButton,this.yesNoButton,this.yesNoCancelButton, this.abortRetryIgnoreButton,this.okCancelButton,this.okButton } ); this.buttonTypeGroupBox.Location =new System.Drawing.Point( 16, 56 ); this.buttonTypeGroupBox.Name = "buttonTypeGroupBox"; this.buttonTypeGroupBox.Size =new System.Drawing.Size( 152, 272 ); this.buttonTypeGroupBox.TabIndex = 0; this.buttonTypeGroupBox.TabStop = false; this.buttonTypeGroupBox.Text = "Button Type"; // // retryCancelButton // this.retryCancelButton.Location =new System.Drawing.Point( 16, 224 ); this.retryCancelButton.Name = "retryCancelButton"; this.retryCancelButton.Size =new System.Drawing.Size( 100, 23 ); this.retryCancelButton.TabIndex = 4; this.retryCancelButton.Text = "RetryCancel"; // all radio buttons for button types are registered // to buttonType_CheckedChanged event handler this.retryCancelButton.CheckedChanged +=new System.EventHandler(this.buttonType_CheckedChanged ); // // yesNoButton // this.yesNoButton.Location = new System.Drawing.Point( 16, 184 ); this.yesNoButton.Name = "yesNoButton"; this.yesNoButton.Size = new System.Drawing.Size( 100, 23 ); this.yesNoButton.TabIndex = 0; this.yesNoButton.Text = "YesNo"; this.yesNoButton.CheckedChanged +=new System.EventHandler(this.buttonType_CheckedChanged ); // // yesNoCancelButton // this.yesNoCancelButton.Location =new System.Drawing.Point( 16, 144 ); this.yesNoCancelButton.Name = "yesNoCancelButton"; this.yesNoCancelButton.Size =new System.Drawing.Size( 100, 23 ); this.yesNoCancelButton.TabIndex = 3; this.yesNoCancelButton.Text = "YesNoCancel"; this.yesNoCancelButton.CheckedChanged +=new System.EventHandler(this.buttonType_CheckedChanged ); // // abortRetryIgnoreButton // this.abortRetryIgnoreButton.Location =new System.Drawing.Point( 16, 104 ); this.abortRetryIgnoreButton.Name ="abortRetryIgnoreButton"; this.abortRetryIgnoreButton.Size =new System.Drawing.Size( 120, 23 ); this.abortRetryIgnoreButton.TabIndex = 2; this.abortRetryIgnoreButton.Text = "AbortRetryIgnore"; this.abortRetryIgnoreButton.CheckedChanged += new System.EventHandler(this.buttonType_CheckedChanged ); // // okCancelButton // this.okCancelButton.Location =new System.Drawing.Point( 16, 64 ); this.okCancelButton.Name = "okCancelButton"; this.okCancelButton.Size =new System.Drawing.Size( 100, 23 ); this.okCancelButton.TabIndex = 1; this.okCancelButton.Text = "OKCancel"; this.okCancelButton.CheckedChanged +=new System.EventHandler(this.buttonType_CheckedChanged ); // // okButton // this.okButton.Checked = true; this.okButton.Location =new System.Drawing.Point( 16, 24 ); this.okButton.Name = "okButton"; this.okButton.Size =new System.Drawing.Size( 100, 23 ); this.okButton.TabIndex = 0; this.okButton.TabStop = true; this.okButton.Text = "OK"; this.okButton.CheckedChanged +=new System.EventHandler(this.buttonType_CheckedChanged ); // // iconTypeGroupBox // this.iconTypeGroupBox.Controls.AddRange(new System.Windows.Forms.Control[] { this.questionButton,this.informationButton,this.exclamationButton, this.errorButton } ); this.iconTypeGroupBox.Location =new System.Drawing.Point( 200, 56 ); this.iconTypeGroupBox.Name = "iconTypeGroupBox"; this.iconTypeGroupBox.Size =new System.Drawing.Size( 136, 176 ); this.iconTypeGroupBox.TabIndex = 1; this.iconTypeGroupBox.TabStop = false; this.iconTypeGroupBox.Text = "Icon"; // // questionButton // this.questionButton.Location =new System.Drawing.Point( 16, 144 ); this.questionButton.Name = "questionButton"; this.questionButton.Size =new System.Drawing.Size( 100, 23 ); this.questionButton.TabIndex = 0; this.questionButton.Text = "Question"; // all radio buttons for icon types are registered // to iconType_CheckedChanged event handler this.questionButton.CheckedChanged +=new System.EventHandler(this.iconType_CheckedChanged ); // // exclamationButton // this.exclamationButton.Location =new System.Drawing.Point( 16, 64 ); this.exclamationButton.Name = "exclamationButton"; this.exclamationButton.Size =new System.Drawing.Size( 104, 23 ); this.exclamationButton.TabIndex = 2; this.exclamationButton.Text = "Exclamation"; this.exclamationButton.CheckedChanged +=new System.EventHandler(this.iconType_CheckedChanged ); // // errorButton // this.errorButton.Location =new System.Drawing.Point( 16, 24 ); this.errorButton.Name = "errorButton"; this.errorButton.Size =new System.Drawing.Size( 100, 23 ); this.errorButton.TabIndex = 1; this.errorButton.Text = "Error"; this.errorButton.CheckedChanged +=new System.EventHandler(this.iconType_CheckedChanged ); // // displayLabel // this.displayLabel.Location =new System.Drawing.Point( 200, 304 ); this.displayLabel.Name = "displayLabel"; this.displayLabel.Size = new System.Drawing.Size( 136, 24 ); this.displayLabel.TabIndex = 4; // // displayButton // this.displayButton.Location =new System.Drawing.Point( 200, 240 ); this.displayButton.Name = "displayButton"; this.displayButton.Size =new System.Drawing.Size( 136, 48 ); this.displayButton.TabIndex = 3; this.displayButton.Text = "Display"; this.displayButton.Click +=new System.EventHandler( this.displayButton_Click ); // // promptLabel // this.promptLabel.Font =new System.Drawing.Font("Microsoft Sans Serif", 9.5F, System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,( ( System.Byte )( 0 ) ) ); this.promptLabel.Location =new System.Drawing.Point( 8, 16 ); this.promptLabel.Name = "promptLabel"; this.promptLabel.Size =new System.Drawing.Size( 344, 24 ); this.promptLabel.TabIndex = 5; this.promptLabel.Text = "Choose the type of MessageBox you would like to display!"; // // RadioButtonsTest // this.AutoScaleBaseSize =new System.Drawing.Size( 5, 13 ); this.ClientSize =new System.Drawing.Size( 360, 341 ); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.promptLabel,this.displayLabel,this.displayButton, this.iconTypeGroupBox,this.buttonTypeGroupBox } ); this.Name = "RadioButtonsTest"; this.Text = "Demonstrating RadioButtons"; this.buttonTypeGroupBox.ResumeLayout( false ); this.iconTypeGroupBox.ResumeLayout( false ); this.ResumeLayout( false ); } [STAThread] static void Main() { Application.Run( new RadioButtonsTest() ); } private void buttonType_CheckedChanged(object sender, System.EventArgs e ) { if ( sender == okButton ) buttonType = MessageBoxButtons.OK; else if ( sender == okCancelButton ) buttonType = MessageBoxButtons.OKCancel; else if ( sender == abortRetryIgnoreButton ) buttonType = MessageBoxButtons.AbortRetryIgnore; else if ( sender == yesNoCancelButton ) buttonType = MessageBoxButtons.YesNoCancel; else if ( sender == yesNoButton ) buttonType = MessageBoxButtons.YesNo; else buttonType = MessageBoxButtons.RetryCancel; } private void iconType_CheckedChanged(object sender, System.EventArgs e ) { if ( sender == errorButton ) iconType = MessageBoxIcon.Error; else if ( sender == exclamationButton ) iconType = MessageBoxIcon.Exclamation; else if ( sender == informationButton ) iconType = MessageBoxIcon.Information; else iconType = MessageBoxIcon.Question; } protected void displayButton_Click(object sender, System.EventArgs e ) { DialogResult result =MessageBox.Show( "This is Your Custom MessageBox.", "Custom MessageBox", buttonType, iconType, 0, 0 ); switch ( result ) { case DialogResult.OK: displayLabel.Text = "OK was pressed."; break; case DialogResult.Cancel: displayLabel.Text = "Cancel was pressed."; break; case DialogResult.Abort: displayLabel.Text = "Abort was pressed."; break; case DialogResult.Retry: displayLabel.Text = "Retry was pressed."; break; case DialogResult.Ignore: displayLabel.Text = "Ignore was pressed."; break; case DialogResult.Yes: displayLabel.Text = "Yes was pressed."; break; case DialogResult.No: displayLabel.Text = "No was pressed."; break; } } }
</source>