Home

.NET Controls: Radio Buttons

 

Introduction

A radio button, sometimes called an option button, is a circular control that comes in a group with other controls of the same type. Each radio button is made of a small empty circle O. From the group, when the user clicks one of them, the radio button that was clicked becomes filled with a dot 8. When one of the radio buttons in the group is selected and displays its dot, the others display empty circles. To guide the user as to what the radio buttons mean, each is accompanied by a label.

Here is an example of a form with three radio buttons: Small, Medium, and Large

To create a radio button, on the Toolbox, you can click the RadioButton control . Alternatively, you can declare a RadioButton class and use the form's constructor to initialize the radio buttons. Because radio buttons always come as a group, you should include them in another control that visibly shows that the radio buttons belong to the same group. The most common control used for this purpose is the group box created using the GroupBox control. Here is an example:

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Exercise
    Inherits Form

    Private grpPizzaSize As System.Windows.Forms.GroupBox
    Private rdoSmall As System.Windows.Forms.RadioButton
    Private rdoMedium As System.Windows.Forms.RadioButton
    Private rdoLarge As System.Windows.Forms.RadioButton

    Public Sub New()
        Me.InitializeComponent()
    End Sub
    Sub InitializeComponent()

        grpPizzaSize = New GroupBox
        grpPizzaSize.Location = New Point(16, 16)
        grpPizzaSize.Size = New Size(136, 128)
        grpPizzaSize.Text = "Pizza Size"

        rdoSmall = New RadioButton
        rdoSmall.Location = New Point(16, 24)
        rdoSmall.Text = "Small"
        grpPizzaSize.Controls.Add(rdoSmall)

        rdoMedium = New RadioButton
        rdoMedium.Location = New Point(16, 56)
        rdoMedium.Text = "Medium"
        grpPizzaSize.Controls.Add(rdoMedium)

        rdoLarge = New RadioButton
        rdoLarge.Location = New Point(16, 88)
        rdoLarge.Text = "Large"
        grpPizzaSize.Controls.Add(rdoLarge)

        Controls.Add(grpPizzaSize)
        Size = New Size(176, 192)
        StartPosition = FormStartPosition.CenterScreen
        Text = "Pizza Order"
        
    End Sub

    Public Shared Sub main()
        Dim exo As Exercise
        exo = New Exercise
        Application.Run(exo)
    End Sub
End Class
 

Characteristics of Radio Buttons

While radio buttons come as a group, only one of them can be selected at a given time. The item that is selected has its Checked property set to True. There are two main ways you can use this property. To select a particular radio button, set its Checked property to True. To find out if an item is selected, get the value of its Checked property. You can also programmatically check a radio button. Here is an example:

rdoMedium.Checked = True

By default, the round box of a radio button control is positioned to the left side of its accompanying label. In .NET Framework applications, you have many options. Besides the left position, the most common alignment consists of positioning the round box to the right side of its label. The position of the round box with regards to its label is controlled by the CheckAlign property which is a value of type ContentAlignment. You can set or change this property programmatically as follows:

rdoSmall.CheckAlign = ContentAlignment.MiddleRight

By default, radio buttons appear as rounded boxes that get filled with a dot when the user selects one. Optionally, you can make a radio button appear as a toggle button. Normally, if you make one radio button appear as a button, you should apply the same characteristics on the other radio buttons. The button would appear as regular rectangular object. When the user clicks such a button, it appears down:

If the user clicks another button, this button becomes up:

To change the appearance of a radio button, assign the Button or Normal value to its Appearance property. The Appearance property is based on the Appearance enumerator. Here is an example:

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Exercise
    Inherits Form

    Private grpPizzaSize As System.Windows.Forms.GroupBox
    Private rdoSmall As System.Windows.Forms.RadioButton
    Private rdoMedium As System.Windows.Forms.RadioButton
    Private rdoLarge As System.Windows.Forms.RadioButton

    Public Sub New()
        Me.InitializeComponent()
    End Sub
    Sub InitializeComponent()

        grpPizzaSize = New GroupBox
        grpPizzaSize.Location = New Point(16, 16)
        grpPizzaSize.Size = New Size(136, 128)
        grpPizzaSize.Text = "Pizza Size"

        rdoSmall = New RadioButton
        rdoSmall.Location = New Point(16, 24)
        rdoSmall.Text = "Small"
        grpPizzaSize.Controls.Add(rdoSmall)

        rdoMedium = New RadioButton
        rdoMedium.Location = New Point(16, 56)
        rdoMedium.Text = "Medium"
        grpPizzaSize.Controls.Add(rdoMedium)

        rdoLarge = New RadioButton
        rdoLarge.Location = New Point(16, 88)
        rdoLarge.Text = "Large"
        grpPizzaSize.Controls.Add(rdoLarge)

        Controls.Add(grpPizzaSize)
        Size = New Size(176, 192)
        StartPosition = FormStartPosition.CenterScreen
        Text = "Pizza Order"

        rdoMedium.Checked = True
        rdoSmall.Appearance = Appearance.Button
        rdoMedium.Appearance = Appearance.Button
        rdoLarge.Appearance = Appearance.Button

    End Sub

    Public Shared Sub main()
        Dim exo As Exercise
        exo = New Exercise
        Application.Run(exo)
    End Sub
End Class

When the user clicks a radio button, the radio button that was clicked fires a Click event.

 

Radio Button Events

If the user clicks a radio button, since this control is primarily a button, the radio button that was clicked in the group fires a Click event. This is the most regularly used event of a radio button. Normally, when the user clicks a button in the group, the round box of that button becomes filled and the Click event is fired. If the user clicks a button that is already checked, nothing changes in the round box of that button but the Click event fires again. In some cases, you may want to execute code only if the checked state of a button has actually changed rather than being interested to know whether the button was clicked or not.

If you are interested only when the checked stated of a button is changed, you can use the CheckedChanged event. This event is fired whenever the checked state of a button is modified. Here is an example of implementing it:

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Public Class Exercise
    Inherits Form

    Private grpPizzaSize As System.Windows.Forms.GroupBox
    Friend WithEvents rdoSmall As System.Windows.Forms.RadioButton
    Friend WithEvents rdoMedium As System.Windows.Forms.RadioButton
    Friend WithEvents rdoLarge As System.Windows.Forms.RadioButton

    Public Sub New()
        Me.InitializeComponent()
    End Sub
    Sub InitializeComponent()

        grpPizzaSize = New GroupBox
        grpPizzaSize.Location = New Point(16, 16)
        grpPizzaSize.Size = New Size(136, 128)
        grpPizzaSize.Text = "Pizza Size"

        rdoSmall = New RadioButton
        rdoSmall.Location = New Point(16, 24)
        '       rdoSmall.TabIndex = 0
        rdoSmall.Text = "Small"
        grpPizzaSize.Controls.Add(rdoSmall)

        rdoMedium = New RadioButton
        rdoMedium.Location = New Point(16, 56)
        '      rdoMedium.TabIndex = 1
        rdoMedium.Text = "Medium"
        grpPizzaSize.Controls.Add(rdoMedium)

        rdoLarge = New RadioButton
        rdoLarge.Location = New Point(16, 88)
        '     rdoLarge.TabIndex = 2
        rdoLarge.Text = "Large"
        grpPizzaSize.Controls.Add(rdoLarge)

        Controls.Add(grpPizzaSize)
        Size = New Size(176, 192)
        StartPosition = FormStartPosition.CenterScreen
        Text = "Pizza Order"

        rdoMedium.Checked = True
        rdoSmall.Appearance = Appearance.Button
        rdoMedium.Appearance = Appearance.Button
        rdoLarge.Appearance = Appearance.Button

    End Sub

    Private Sub rdoSmall_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoSmall.CheckedChanged
        MsgBox("Small was clicked")
    End Sub

    Private Sub rdoMedium_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoMedium.CheckedChanged
        MsgBox("Medium was clicked")
    End Sub

    Private Sub rdoLarge_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoLarge.CheckedChanged
        MsgBox("Large was clicked")
    End Sub

    Public Shared Sub main()
        Dim exo As Exercise
        exo = New Exercise
        Application.Run(exo)
    End Sub
End Class

If you configure your application and give the user the ability to change the appearance of the radio button from a round circle to a rectangular object and vice-versa, and if the user decides to change this appearance, when this is done, the control whose appearance was changed fires an AppearanceChanged event. The AppearanceChanged event is of type EventArgs, meaning that it doesn't carry any significant information other than to let you know that the appearance of the button was changed.

 

Home Copyright © 2005-2012 FunctionX