Visual Basic .NET - RadioButton Control

The RadioButton control in VB.NET is a user interface control that allows the user to select one option from a group of options. Here are some common properties, methods, and events of the RadioButton control:

Common Properties:

  • Checked: Gets or sets a value indicating whether the radio button is checked.
  • Text: Gets or sets the text associated with the radio button.

Common Events:

  • CheckedChanged: Occurs when the Checked property value changes.

Here is an example of how to use the RadioButton control in VB.NET:

  • Create a new Windows Forms application in Visual Studio.
  • Drag and drop a GroupBox control onto the form.
  • Drag and drop two RadioButton controls into the GroupBox control.
  • Set the Text property of the first RadioButton control to "Male" and the Text property of the second RadioButton control to "Female".
  • Double-click on one of the RadioButton controls to create a CheckedChanged event handler.

In the event handler, add the following code:

Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
    If RadioButton1.Checked Then
        MessageBox.Show("You selected Male.")
    End If
End Sub

Repeat the previous step for the other RadioButton control, changing the message box text to "You selected Female."

Run the application and select one of the radio buttons to see the message box display the selected option.

This example demonstrates how to use the CheckedChanged event to determine which RadioButton control was selected by the user. The Checked property is used to determine if the RadioButton is checked or not.

Suppose you have a form with a RadioButton control named "radioButton1" and a Label control named "label1". You want to display a message in the label when the user selects a RadioButton option. You can use the CheckedChanged event to achieve this:

Private Sub radioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles radioButton1.CheckedChanged
    If radioButton1.Checked Then
        label1.Text = "You selected Option 1"
    Else
        label1.Text = "Option 1 is not selected"
    End If
End Sub

In this example, the CheckedChanged event is handled by the radioButton1_CheckedChanged method. Inside the method, we use the Checked property to determine if the RadioButton is selected or not. If it is selected, we set the text of label1 to "You selected Option 1", otherwise we set it to "Option 1 is not selected".

You can similarly handle the CheckedChanged event for other RadioButton controls and perform different actions depending on which RadioButton option is selected.