The RadioButton control is used to provide a set of mutually exclusive options. The user can select one radio button in a group. If you need to place more than one group of radio buttons in the same form, you should place them in different container controls like a GroupBox control.
Let's create three radio buttons by dragging RadioButton controls from the Toolbox and dropping on the form.
The Checked property of the radio button is used to set the state of a radio button. You can display text, image or both on radio button control. You can also change the appearance of the radio button control by using the Appearance property.
The following are some of the commonly used properties of the RadioButton control −
|Sr.No.||Property & Description|
Gets or sets a value determining the appearance of the radio button.
Gets or sets a value indicating whether the Checked value and the appearance of the control automatically change when the control is clicked.
Gets or sets the location of the check box portion of the radio button.
Gets or sets a value indicating whether the control is checked.
Gets or sets the caption for a radio button.
Gets or sets a value indicating whether a user can give focus to the RadioButton control using the TAB key.
The following are some of the commonly used methods of the RadioButton control −
|Sr.No.||Method Name & Description|
Generates a Click event for the control, simulating a click by a user.
The following are some of the commonly used events of the RadioButton control −
|Sr.No||Event & Description|
Occurs when the value of the Appearance property of the RadioButton control is changed.
Occurs when the value of the Checked property of the RadioButton control is changed.
Consult Microsoft documentation for detailed list of properties, methods and events of the RadioButton control.
In the following example, let us create two groups of radio buttons and use their CheckedChanged events for changing the BackColor and ForeColor property of the form.
Let's double click on the radio buttons and put the follow code in the opened window.
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Set the caption bar text of the form. Me.Text = "tutorialspont.com" End Sub Private Sub RadioButton1_CheckedChanged(sender As Object, _ e As EventArgs) Handles RadioButton1.CheckedChanged Me.BackColor = Color.Red End Sub Private Sub RadioButton2_CheckedChanged(sender As Object, _ e As EventArgs) Handles RadioButton2.CheckedChanged Me.BackColor = Color.Green End Sub Private Sub RadioButton3_CheckedChanged(sender As Object, _ e As EventArgs) Handles RadioButton3.CheckedChanged Me.BackColor = Color.Blue End Sub Private Sub RadioButton4_CheckedChanged(sender As Object, _ e As EventArgs) Handles RadioButton4.CheckedChanged Me.ForeColor = Color.Black End Sub Private Sub RadioButton5_CheckedChanged(sender As Object, _ e As EventArgs) Handles RadioButton5.CheckedChanged Me.ForeColor = Color.White End Sub Private Sub RadioButton6_CheckedChanged(sender As Object, _ e As EventArgs) Handles RadioButton6.CheckedChanged Me.ForeColor = Color.Red End Sub End Class
When the above code is executed and run using Start button available at the Microsoft Visual Studio tool bar, it will show the following window −