Export (0) Print
Expand All

RadioButton Class

Updated: October 2010

Enables the user to select a single option from a group of choices when paired with other RadioButton controls.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
[DefaultBindingPropertyAttribute("Checked")]
public class RadioButton : ButtonBase

The RadioButton control can display text, an Image, or both.

When the user selects one option button (also known as a radio button) within a group, the others clear automatically. All RadioButton controls in a given container, such as a Form, constitute a group. To create multiple groups on one form, place each group in its own container, such as a GroupBox or Panel control.

RadioButton and CheckBox controls have a similar function: they offer choices a user can select or clear. The difference is that multiple CheckBox controls can be selected at the same time, but option buttons are mutually exclusive.

Use the Checked property to get or set the state of a RadioButton. The option button's appearance can be altered to appear as a toggle-style button or as a standard option button by setting the Appearance property.

The following code example creates and initializes two RadioButton controls in a GroupBox. The example uses the CheckedChanged event to track which RadioButton is selected and reports the text of the selected RadioButton when the user clicks a Button. To run this example, paste the code into a Windows Form. Call InitializeRadioButtons from the form's constructor or the Load event-handling method.

private GroupBox groupBox1;
private RadioButton radioButton2;
private RadioButton radioButton1;
private RadioButton selectedrb;
private Button getSelectedRB;

public void InitializeRadioButtons()
{
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.radioButton2 = new System.Windows.Forms.RadioButton();
    this.radioButton1 = new System.Windows.Forms.RadioButton();
    this.getSelectedRB = new System.Windows.Forms.Button();

    this.groupBox1.Controls.Add(this.radioButton2);
    this.groupBox1.Controls.Add(this.radioButton1);

    this.groupBox1.Controls.Add(this.getSelectedRB);
    this.groupBox1.Location = new System.Drawing.Point(30, 100);
    this.groupBox1.Size = new System.Drawing.Size(220, 125);
    this.groupBox1.Text = "Radio Buttons";

    this.radioButton2.Location = new System.Drawing.Point(31, 53);
    this.radioButton2.Size = new System.Drawing.Size(67, 17);
    this.radioButton2.Text = "Choice 2";
    this.radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);

    this.radioButton1.Location = new System.Drawing.Point(31, 20);
    this.radioButton1.Name = "radioButton1";
    this.radioButton1.Size = new System.Drawing.Size(67, 17);
    this.radioButton1.Text = "Choice 1";
    this.radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);

    this.getSelectedRB.Location = new System.Drawing.Point(10, 75);
    this.getSelectedRB.Size = new System.Drawing.Size(200, 25);
    this.getSelectedRB.Text = "Get selected RadioButton";
    this.getSelectedRB.Click += new EventHandler(getSelectedRB_Click);

    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.groupBox1);
}

void radioButton_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rb = sender as RadioButton;

    if (rb == null)
    {
        MessageBox.Show("Sender is not a RadioButton");
        return;
    }

    // Ensure that the RadioButton.Checked property 
    // changed to true. 
    if (rb.Checked)
    {
        // Keep track of the selected RadioButton by saving a reference 
        // to it.
        selectedrb = rb;
    }
}

// Show the text of the selected RadioButton. 
void getSelectedRB_Click(object sender, EventArgs e)
{
    MessageBox.Show(selectedrb.Text);
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

Date

History

Reason

October 2010

Revised example to demonstrate getting the selected RadioButton.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft