This tutorial will introduce to us what is a radio button control in vb.net together with a sample source code.
What is a radio button?
A radio button is a gui (graphical user interface) object or control that allows the user to only select one option from a given set of choices.
This is a simple program in vb.net that allows the user to select his or her favorite programming language in a given set of list in a form of radio button. The selected option will be automatically display in a textbox.
Kindly follow this simple steps.
- Open your visual studio 2010 or higher version.
- Click File then New Project.
- Select Visual Basic then Windows Form Application.
- Add the following to the form.
- 1 group box
- 5 radio buttons
- 2 labels
- Sample form design
- On the Load event of the form, paste the line of codes below:
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
RadioButton4.Checked = False
RadioButton5.Checked = False
Code Explanation:
This will set all the radio buttons to unchecked mode or value.
The code now should be like these:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
RadioButton4.Checked = False
RadioButton5.Checked = False
End Sub
- Double click the first radio button (with VB.net caption) and paste the code below.
If RadioButton1.Checked = True Then
Label2.Text = RadioButton1.Text
End If
When you double click the radio control, it will proceed to CheckedChanged event
Full code:
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
Label2.Text = RadioButton1.Text
End If
End Sub
Code Explanation:
If the user selects the first radio button the caption or text of the label2 will be equal to the text of the radiobutton1 which is VB.net.
Do it on the remaining radio buttons.
Just change the value of RadioButton1 to the name of the radio button
Codes for the remaining radio buttons
'for Java
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = True Then
Label2.Text = RadioButton2.Text
End If
End Sub
'for C#
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
If RadioButton3.Checked = True Then
Label2.Text = RadioButton3.Text
End If
End Sub
'for PHP
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
If RadioButton4.Checked = True Then
Label2.Text = RadioButton4.Text
End If
End Sub
'for Others
Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged
If RadioButton5.Checked = True Then
Label2.Text = RadioButton5.Text
End If
End Sub
- Save and Run the project.