Working with Forms and Controls in VB.NET: RadioButton Control
The RadioButton control in VB.NET is used to present a list of mutually exclusive options to the user, where only one option can be selected at a time. The RadioButton control has a radio button next to each option that indicates whether that option is selected or not.
Syntax
The RadioButton control is created in the same way as other Windows Forms controls in VB.NET. The syntax to create a RadioButton control is:
Dim rb As New RadioButton
Example
Here is an example that demonstrates how to create a simple RadioButton control:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rb1 As New RadioButton
rb1.Name = "rb1"
rb1.Text = "Option 1"
rb1.Checked = True
rb1.Location = New Point(10, 10)
Me.Controls.Add(rb1)
Dim rb2 As New RadioButton
rb2.Name = "rb2"
rb2.Text = "Option 2"
rb2.Checked = False
rb2.Location = New Point(10, 30)
Me.Controls.Add(rb2)
End Sub
Output
The output of the above example will show two radio buttons on the form, the first one labeled "Option 1" and selected by default, and the second one labeled "Option 2" and unselected.
Explanation
In the above example, we have created two RadioButton controls rb1
and rb2
and added them to the form Me.Controls.Add(rb)
. We have also set their Name
, Text
, Checked
and Location
properties. Here, the first radio button rb1
is selected by default as Checked
property is set to True
.
Use
The RadioButton control is commonly used when we want the user to select only one option from a list of options. It is often used in combination with other controls like a GroupBox or ListBox to present a list of mutually exclusive options to the user.
Important Points
- The RadioButton control is used to present a list of mutually exclusive options to the user.
- Only one option can be selected at a time.
- The
Checked
property of the RadioButton control is used to determine which option is selected. - The RadioButton control is often used in combination with other controls like a GroupBox or ListBox.
Summary
In summary, the RadioButton control in VB.NET is used to present a list of mutually exclusive options to the user. It is highly useful when we want the user to select only one option from a list of options, and we can use it in combination with other controls like a GroupBox or ListBox to present the user with a list of options.