Saturday, 14 September 2013

VB.Net - RadioButton Control

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.
VB.Net Radio Button
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.

Properties of the RadioButton Control

The following are some of the commonly used properties of the RadioButton control:
S.NPropertyDescription
1AppearanceGets or sets a value determining the appearance of the radio button.
2AutoCheckGets or sets a value indicating whether the Checked value and the appearance of the control automatically change when the control is clicked.
3CheckAlignGets or sets the location of the check box portion of the radio button.
4CheckedGets or sets a value indicating whether the control is checked.
5TextGets or sets the caption for a radio button.
6TabStopGets or sets a value indicating whether a user can give focus to the RadioButton control using the TAB key.

Methods of the RadioButton Control

The following are some of the commonly used methods of the RadioButton control:
S.NMethod Name & Description
1PerformClick
Generates a Click event for the control, simulating a click by a user.

Events of the RadioButton Control

The following are some of the commonly used events of the RadioButton control:
S.NEventDescription
1AppearanceChangedOccurs when the value of the Appearance property of the RadioButton control is changed.
2CheckedChangedOccurs 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.

Example

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.
Radio Button Example Result 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 following window:
Result Form

No comments:

Post a Comment