RadioButton Control

Radio Button Control - UK Academe

RadioButton controls for Windows Forms present the user with a set of two or more mutually exclusive choices. While radio buttons and check boxes may appear to function similarly, there is an important difference: when a user selects a radio button, the other radio buttons in the same group cannot be selected as well.

In contrast, any number of check boxes can be selected. Defining a group of radio buttons tells the user, "This is a set of choices that can be selected from only one group."

When you click on a RadioButton control, its Checked property is set to true and the Click event handler is called.


radioButton1.Checked = true;

When the value of the checked property changes, the CheckedChanged event will be raised. If the AutoCheck property is set to true (the default), all others in the group will be automatically cleared when the radio button is selected.

Usually this property is set to false only when validation code is used to ensure that the selected radio button is an option that is permissible.


using System;
using System.Drawing;
using System.Windows.Forms;

namespace RadioButtonControl
{
    public partial class frmRadioButton : Form
    {
        string msg = "";
        public frmRadioButton()
        {
            InitializeComponent();
        }

        private void rdbBlack_CheckedChanged(object sender, EventArgs e)
        {
            if(rdbBlack.Checked==true)
            {
                msg = "Black radio is selected";
                panColor.BackColor = Color.Black;
            }
            else
            {
                msg = "";
                panColor.BackColor = Color.White;
            }
        }

        private void btnMessage_Click(object sender, EventArgs e)
        {
            MessageBox.Show(msg);
        }

        private void rdbRed_CheckedChanged(object sender, EventArgs e)
        {
            if(rdbRed.Checked==true)
            {
                msg = "Red radio button is selected";
                panColor.BackColor = Color.Red;
            }
            else
            {
                msg = "";
                panColor.BackColor = Color.White;
            }
        }
        private void rdbGreen_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbGreen.Checked == true)
            {
                msg = "Green radio button is selected";
                panColor.BackColor = Color.Green;
            }
            else
            {
                msg = "";
                panColor.BackColor = Color.White;
            }
        }
    }
}

Radio Button Control

Video Tutorial