RadioButton Class
Updated: October 2010
Enables the user to select a single option from a group of choices when paired with other RadioButton controls.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
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 groupBox1 As GroupBox Private radioButton2 As RadioButton Private radioButton1 As RadioButton Private selectedrb As RadioButton Private WithEvents getSelectedRB As Button Public Sub InitializeRadioButtons() Me.groupBox1 = New System.Windows.Forms.GroupBox() Me.radioButton2 = New System.Windows.Forms.RadioButton() Me.radioButton1 = New System.Windows.Forms.RadioButton() Me.getSelectedRB = New System.Windows.Forms.Button() Me.groupBox1.Controls.Add(Me.radioButton2) Me.groupBox1.Controls.Add(Me.radioButton1) Me.groupBox1.Controls.Add(Me.getSelectedRB) Me.groupBox1.Location = New System.Drawing.Point(30, 100) Me.groupBox1.Size = New System.Drawing.Size(220, 125) Me.groupBox1.Text = "Radio Buttons" Me.radioButton2.Location = New System.Drawing.Point(31, 53) Me.radioButton2.Size = New System.Drawing.Size(67, 17) Me.radioButton2.Text = "Choice 2" AddHandler Me.radioButton2.Click, AddressOf radioButton_CheckedChanged Me.radioButton1.Location = New System.Drawing.Point(31, 20) Me.radioButton1.Name = "radioButton1" Me.radioButton1.Size = New System.Drawing.Size(67, 17) Me.radioButton1.Text = "Choice 1" AddHandler Me.radioButton1.Click, AddressOf radioButton_CheckedChanged Me.getSelectedRB.Location = New System.Drawing.Point(10, 75) Me.getSelectedRB.Size = New System.Drawing.Size(200, 25) Me.getSelectedRB.Text = "Get selected RadioButton" 'Me.getSelectedRB.Click += New EventHandler(AddressOf getSelectedRB_Click) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.groupBox1) End Sub Private Sub radioButton_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) _ 'Handles me.radioButton1.CheckChanged, me.radioButton2.CheckChenged Dim rb As RadioButton = TryCast(sender, RadioButton) If rb Is Nothing Then MessageBox.Show("Sender is not a RadioButton") Return End If ' Ensure that the RadioButton.Checked property ' changed to true. If rb.Checked Then ' Keep track of the selected RadioButton by saving a reference ' to it. selectedrb = rb End If End Sub ' Show the text of the selected RadioButton. Private Sub getSelectedRB_Click(ByVal sender As Object, ByVal e As EventArgs) _ Handles getSelectedRB.Click MessageBox.Show(selectedrb.Text) End Sub
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ButtonBase
System.Windows.Forms.RadioButton
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.