Radio Button:
A RadioButton control offers a circular user interface element for
choosing a single option from a set of options. Typically organized within a
group on a container control like a Panel or GroupBox, users can select one
option at a time.
In C#, you can use the RadioButton control from the Windows Forms
library to create radio buttons. Here's a basic example of how you can use
radio buttons in a Windows Forms application:
You have the option to create a RadioButton control through two
approaches: design-time or run-time (dynamically).
For design-time creation, you can easily incorporate a RadioButton
control by dragging and dropping it from the Toolbox onto a Form within Visual
Studio. Upon placement, the RadioButton appears as depicted in Figure 1.
Subsequently, you have the flexibility to manipulate its position and size
through mouse actions, as well as configure its properties and events.
Alternatively, you can dynamically generate a RadioButton control during
run-time using the RadioButton class in code. This method allows for
programmatic creation and customization of RadioButton instances as needed.
Generating a RadioButton control at run-time involves the
straightforward process of instantiating the RadioButton class, configuring its
properties, and subsequently adding the instance to the Form's controls
collection.
To initiate the creation of a dynamic RadioButton, the initial step is
to instantiate an object of the RadioButton class. The subsequent code snippet
accomplishes this:
// Creating a dynamic RadioButton control
RadioButton dynamicRadioButton = new RadioButton();
This code creates an instance of the RadioButton class named
dynamicRadioButton. Following this step, you can proceed to customize its
appearance and behavior by setting various properties. Finally, the dynamic
RadioButton can be added to the Form's controls collection.
this.radioButton1.Location = new
System.Drawing.Point(20, 30);
this.radioButton1.Name =
"radioButton1";
this.radioButton1.Size =
new System.Drawing.Size(90, 17);
this.radioButton1.TabIndex
= 0;
this.radioButton1.TabStop
= true;
this.radioButton1.Text =
"Option 1";
partial class Form1
{
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 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.SuspendLayout();
// groupBox1
this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.radioButton3);
this.groupBox1.Location = new System.Drawing.Point(12, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(200, 120);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Options";
// radioButton1
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(20, 30);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(90, 17);
this.radioButton1.TabIndex = 0;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "Option 1";
// radioButton2
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(20, 55);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(90, 17);
this.radioButton2.TabIndex = 1;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "Option 2";
// radioButton3
this.radioButton3.AutoSize = true;
this.radioButton3.Location = new System.Drawing.Point(20, 80);
this.radioButton3.Name = "radioButton3";
this.radioButton3.Size = new System.Drawing.Size(90, 17);
this.radioButton3.TabIndex = 2;
this.radioButton3.TabStop = true;
this.radioButton3.Text = "Option 3";
// Form1
this.ClientSize = new System.Drawing.Size(224, 156);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "Radio Button Example";
this.ResumeLayout(false);
}
}
Tags:
C#