Csharp/C Sharp by API/System.Windows.Forms/RadioButton
Содержание
RadioButton.Appearance
<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>
RadioButton.Checked
<source lang="csharp">
using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class CarConfigForm : System.Windows.Forms.Form { private System.Windows.Forms.ToolTip calendarTip; private System.Windows.Forms.Label label5; protected System.Windows.Forms.MonthCalendar monthCalendar; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label1; protected System.Windows.Forms.ruboBox comboSalesPerson; protected System.Windows.Forms.ListBox carMakeList; protected System.Windows.Forms.CheckedListBox checkedBoxRadioOptions; protected System.Windows.Forms.Button btnOrder; protected System.Windows.Forms.CheckBox checkFloorMats; protected System.Windows.Forms.RadioButton radioPink; protected System.Windows.Forms.RadioButton radioYellow; protected System.Windows.Forms.RadioButton radioRed; protected System.Windows.Forms.RadioButton radioGreen; protected System.Windows.Forms.GroupBox groupBox1; public CarConfigForm() { InitializeComponent(); CenterToScreen(); } private void InitializeComponent() { this.ruboSalesPerson = new System.Windows.Forms.ruboBox (); this.radioRed = new System.Windows.Forms.RadioButton (); this.groupBox1 = new System.Windows.Forms.GroupBox (); this.radioPink = new System.Windows.Forms.RadioButton (); this.monthCalendar = new System.Windows.Forms.MonthCalendar (); this.checkedBoxRadioOptions = new System.Windows.Forms.CheckedListBox (); this.calendarTip = new System.Windows.Forms.ToolTip (new System.ruponentModel.Container()); this.radioYellow = new System.Windows.Forms.RadioButton (); this.label5 = new System.Windows.Forms.Label (); this.carMakeList = new System.Windows.Forms.ListBox (); this.radioGreen = new System.Windows.Forms.RadioButton (); this.checkFloorMats = new System.Windows.Forms.CheckBox (); this.label3 = new System.Windows.Forms.Label (); this.btnOrder = new System.Windows.Forms.Button (); this.label1 = new System.Windows.Forms.Label (); this.label2 = new System.Windows.Forms.Label (); comboSalesPerson.Location = new System.Drawing.Point (16, 80); comboSalesPerson.Size = new System.Drawing.Size (128, 21); comboSalesPerson.TabIndex = 1; comboSalesPerson.Items.AddRange(new object[4] {"A", "B", "C", "D"}); radioRed.Location = new System.Drawing.Point (264, 24); radioRed.Text = "Red"; radioRed.Size = new System.Drawing.Size (64, 23); radioRed.TabIndex = 2; radioRed.TabStop = true; radioRed.BackColor = System.Drawing.SystemColors.ControlLight; groupBox1.Location = new System.Drawing.Point (8, 120); groupBox1.TabIndex = 4; groupBox1.TabStop = false; groupBox1.Text = "Exterior Color"; groupBox1.Size = new System.Drawing.Size (432, 56); groupBox1.Leave += new System.EventHandler (this.groupBox1_Leave); groupBox1.Enter += new System.EventHandler (this.groupBox1_Enter); radioPink.Location = new System.Drawing.Point (176, 24); radioPink.Text = "Pink"; radioPink.Size = new System.Drawing.Size (56, 23); radioPink.TabIndex = 3; radioPink.TabStop = true; radioPink.BackColor = System.Drawing.SystemColors.ControlLight; monthCalendar.Location = new System.Drawing.Point (20, 224); calendarTip.SetToolTip (monthCalendar, "Please select the date (or dates)"); monthCalendar.TabIndex = 10; monthCalendar.TabStop = true; checkedBoxRadioOptions.Location = new System.Drawing.Point (168, 32); checkedBoxRadioOptions.Cursor = Cursors.Hand; checkedBoxRadioOptions.Size = new System.Drawing.Size (152, 64); checkedBoxRadioOptions.CheckOnClick = true; checkedBoxRadioOptions.TabIndex = 2; checkedBoxRadioOptions.Items.AddRange(new object[6] {"1", "2", "3", "4", "5", "6"}); calendarTip.Active = true; radioYellow.Location = new System.Drawing.Point (96, 24); radioYellow.Text = "Yellow"; radioYellow.Size = new System.Drawing.Size (56, 23); radioYellow.TabIndex = 1; radioYellow.TabStop = true; radioYellow.BackColor = System.Drawing.SystemColors.ControlLight; label5.Location = new System.Drawing.Point (20, 200); label5.Text = "Delivery Date:"; label5.Size = new System.Drawing.Size (184, 16); label5.Font = new System.Drawing.Font ("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Bold); label5.TabIndex = 12; carMakeList.Location = new System.Drawing.Point (328, 32); carMakeList.Size = new System.Drawing.Size (112, 56); carMakeList.ScrollAlwaysVisible = true; carMakeList.TabIndex = 3; carMakeList.Sorted = true; carMakeList.Items.AddRange(new object[9] {"A", "B", "C", "D", "E", "F", "G", "H", "I"}); radioGreen.Location = new System.Drawing.Point (16, 24); radioGreen.Text = "Green"; radioGreen.Size = new System.Drawing.Size (64, 23); radioGreen.TabIndex = 0; radioGreen.TabStop = true; radioGreen.BackColor = System.Drawing.SystemColors.ControlLight; checkFloorMats.Location = new System.Drawing.Point (16, 16); checkFloorMats.Text = "Extra Floor Mats"; checkFloorMats.Size = new System.Drawing.Size (112, 24); checkFloorMats.FlatStyle = System.Windows.Forms.FlatStyle.Popup; checkFloorMats.TabIndex = 0; label3.Location = new System.Drawing.Point (328, 8); label3.Text = "Make:"; label3.Size = new System.Drawing.Size (112, 16); label3.Font = new System.Drawing.Font ("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Bold); label3.TabIndex = 9; btnOrder.Location = new System.Drawing.Point (8, 440); btnOrder.Size = new System.Drawing.Size (120, 32); btnOrder.TabIndex = 6; btnOrder.Text = "Confirm Order"; btnOrder.Click += new System.EventHandler (this.btnOrder_Click); label1.Location = new System.Drawing.Point (16, 56); label1.Text = "Sales Person"; label1.Size = new System.Drawing.Size (144, 24); label1.TabIndex = 7; label2.Location = new System.Drawing.Point (176, 8); label2.Text = "Radio Options:"; label2.Size = new System.Drawing.Size (144, 16); label2.Font = new System.Drawing.Font ("Microsoft Sans Serif", 9, System.Drawing.FontStyle.Bold); label2.TabIndex = 8; this.Text = "Car Configurator"; this.AutoScaleBaseSize = new System.Drawing.Size (5, 13); this.ClientSize = new System.Drawing.Size (456, 485); groupBox1.Controls.Add (this.radioPink); groupBox1.Controls.Add (this.radioYellow); groupBox1.Controls.Add (this.radioRed); groupBox1.Controls.Add (this.radioGreen); this.Controls.Add (this.label5); this.Controls.Add (this.monthCalendar); this.Controls.Add (this.label3); this.Controls.Add (this.label2); this.Controls.Add (this.label1); this.Controls.Add (this.ruboSalesPerson); this.Controls.Add (this.carMakeList); this.Controls.Add (this.checkedBoxRadioOptions); this.Controls.Add (this.btnOrder); this.Controls.Add (this.checkFloorMats); this.Controls.Add (this.groupBox1); } static void Main() { Application.Run(new CarConfigForm()); } protected void btnOrder_Click (object sender, System.EventArgs e) { if(comboSalesPerson.Text != "") Console.WriteLine("Sales Person: " + comboSalesPerson.Text); else Console.WriteLine("You did not select a sales person!"); if(carMakeList.SelectedItem != null) Console.WriteLine("Make: " + carMakeList.SelectedItem); if(checkFloorMats.Checked) Console.WriteLine("You want floor mats."); if(radioRed.Checked) Console.WriteLine("You want a red exterior."); if(radioYellow.Checked) Console.WriteLine("You want a yellow exterior."); if(radioGreen.Checked) Console.WriteLine("You want a green exterior."); if(radioPink.Checked) Console.WriteLine("Why do you want a PINK exterior?"); for(int i = 0; i < checkedBoxRadioOptions.Items.Count; i++) { if(checkedBoxRadioOptions.GetItemChecked(i)) { Console.WriteLine("Radio Item: "+ checkedBoxRadioOptions.Items[i]); } } DateTime startD = monthCalendar.SelectionStart; DateTime endD = monthCalendar.SelectionEnd; string dateStartStr = startD.Date.ToShortDateString(); string dateEndStr = endD.Date.ToShortDateString(); if(dateStartStr != dateEndStr) { Console.WriteLine("Date between" + dateStartStr + " and" + dateEndStr); } else{ Console.WriteLine("You chose a single date:" + dateStartStr); } } protected void groupBox1_Leave (object sender, System.EventArgs e) { groupBox1.Text = "Exterior Color: Thanks for visiting the group..."; } protected void groupBox1_Enter (object sender, System.EventArgs e) { groupBox1.Text = "Exterior Color: You are in the group..."; } } </source>
RadioButton.CheckedChanged
<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.Image
<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>
RadioButton.ImageAlign
<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.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.RadioButton radioButton3; public Form1() { 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(); 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.Size = new System.Drawing.Size(160, 120); this.groupBox1.TabStop = false; this.groupBox1.Text = "Group 1"; 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"; 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.Size = new System.Drawing.Size(120, 24); this.radioButton2.Text = "Option2"; this.radioButton2.TextAlign = System.Drawing.ContentAlignment.MiddleRight; 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.Size = new System.Drawing.Size(120, 24); this.radioButton1.Text = "Option1"; this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); 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.Text = "Radio Button"; this.Load += new System.EventHandler(this.Form1_Load); this.groupBox1.ResumeLayout(false); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { Image img = Image.FromFile("EYE.ICO"); radioButton1.Image = img; radioButton1.ImageAlign = ContentAlignment.MiddleRight; img = Image.FromFile("WRENCH.ICO"); radioButton2.Image = img; 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>