CheckBox and RadioButton
A check box is a choice that stands on its own: on or off, and sometimes unknown. A radio button is a choice among several, exactly one of which is on. The two controls look different and behave differently, and the difference that matters most is about containers, not about properties.
CheckBox
using System;
using System.Drawing;
using System.Windows.Forms;
internal static class Program
{
[STAThread]
private static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
internal sealed class Form1 : Form
{
private readonly CheckBox _wrap = new()
{
Text = "Wrap long lines",
Location = new Point(16, 16),
AutoSize = true,
Checked = true,
};
private readonly CheckBox _readOnly = new()
{
Text = "Open the file read-only",
Location = new Point(16, 44),
AutoSize = true,
};
private readonly CheckBox _backup = new()
{
Text = "Keep a backup",
Location = new Point(16, 72),
AutoSize = true,
ThreeState = true,
CheckState = CheckState.Indeterminate,
};
private readonly Label _summary = new()
{
Location = new Point(16, 110),
AutoSize = true,
};
public Form1()
{
Text = "Check boxes";
ClientSize = new Size(360, 150);
_wrap.CheckedChanged += Describe;
_readOnly.CheckedChanged += Describe;
_backup.CheckStateChanged += Describe;
Controls.Add(_wrap);
Controls.Add(_readOnly);
Controls.Add(_backup);
Controls.Add(_summary);
Describe(this, EventArgs.Empty);
}
private void Describe(object? sender, EventArgs e)
{
_summary.Text = $"wrap={_wrap.Checked}, read-only={_readOnly.Checked}, backup={_backup.CheckState}";
}
}
dotnet build app.csproj
Build succeeded.
0 Warning(s)
0 Error(s)
The listing is built and not run. The first two boxes are the ordinary two-state control. The third sets ThreeState = true and starts at CheckState.Indeterminate, which draws the box with a filled square instead of a tick; a click moves it to the next of the three values. AutoCheck is true by default, which is what makes the click change the state before the event is raised.
Three states
Three states are for a setting the application has not decided: "keep a backup" for a folder whose files disagree, "bold" for a selection that mixes bold and plain text. The control reports them, and the code has to treat the third one explicitly, because Checked is false for both Unchecked and Indeterminate:
- CheckState.Unchecked: Checked is false.
- CheckState.Checked: Checked is true.
- CheckState.Indeterminate: Checked is false, and only CheckState tells it apart from the first case.
- With ThreeState, CheckStateChanged reports every move between the three values, while CheckedChanged fires only when Checked itself changes: a move between Unchecked and Indeterminate leaves it false and raises no CheckedChanged.
RadioButton
A radio button is on or off, and its rule is: exactly one of the buttons in the same container is on. The container is the form or any other control that holds them, such as a GroupBox or a Panel. Two radio buttons on one form are one group; two on two panels are two groups.
using System;
using System.Drawing;
using System.Windows.Forms;
internal sealed class Form1 : Form
{
private readonly RadioButton _daily = new() { Text = "Daily", Location = new Point(16, 16) };
private readonly RadioButton _weekly = new() { Text = "Weekly", Location = new Point(16, 44) };
private readonly RadioButton _monthly = new() { Text = "Monthly", Location = new Point(16, 72) };
public Form1()
{
Text = "One of three";
ClientSize = new Size(300, 120);
_weekly.Checked = true;
Controls.Add(_daily);
Controls.Add(_weekly);
Controls.Add(_monthly);
}
}
That block is a fragment: a form with no Main, so the checker counts it and does not build it. Setting Checked to true on one button turns the others off, and a click turns the clicked one on and the previous one off. The initial state is a decision the form has to make: without one, a form with three radio buttons and none of them checked is valid and unhelpful.
The two controls meet in a property grid: a RadioButton that is checked and then disabled shows the choice without allowing a change, and a CheckBox needs no group because each box is independent.
CheckedChanged
Both controls raise CheckedChanged when their state changes, and it fires once per change, not once per click. That makes it the place for the reaction the user expects immediately, such as showing a password field only when "show" is ticked. A batch of boxes that all matter together is better read at the moment of use:
using System;
using System.Drawing;
using System.Windows.Forms;
internal static class Program
{
[STAThread]
private static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
internal sealed class Form1 : Form
{
private readonly CheckBox _bold = new() { Text = "Bold", Location = new Point(16, 16), AutoSize = true };
private readonly CheckBox _italic = new() { Text = "Italic", Location = new Point(16, 44), AutoSize = true };
private readonly Label _sample = new() { Location = new Point(16, 84), AutoSize = true, Text = "Sample text" };
public Form1()
{
Text = "Font style";
ClientSize = new Size(300, 140);
_bold.CheckedChanged += ApplyStyle;
_italic.CheckedChanged += ApplyStyle;
Controls.Add(_bold);
Controls.Add(_italic);
Controls.Add(_sample);
ApplyStyle(this, EventArgs.Empty);
}
private void ApplyStyle(object? sender, EventArgs e)
{
var style = FontStyle.Regular;
if (_bold.Checked)
{
style |= FontStyle.Bold;
}
if (_italic.Checked)
{
style |= FontStyle.Italic;
}
_sample.Font = new Font(_sample.Font, style);
}
}
dotnet build app.csproj
Build succeeded.
0 Warning(s)
0 Error(s)
One handler reads both boxes, so the order in which the user ticks them cannot matter. FontStyle is a [Flags] enumeration, which is why the two booleans combine with | instead of a chain of if branches.
Exercises
- Add a fourth check box whose state depends on the other two, and decide what Indeterminate means for it.
- Put the two check boxes of the font example inside a group box and say whether the behaviour changes.
- Set AutoCheck to false on one box and write the handler that changes the state itself.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What decides which radio buttons belong to one group?
Answer
Their container: the form, a GroupBox or a Panel
Radio buttons group by parent control, so a GroupBox is how a form has two independent groups.
-
What does CheckBox.Checked return when CheckState is Indeterminate?
Answer
false
Checked is a two-state view; CheckState is the only way to see the third state.
-
Which event reports a change of CheckState, including the indeterminate one?
Answer
CheckStateChanged
CheckedChanged is the two-state event; CheckStateChanged sees all three.
-
What does AutoCheck do on a CheckBox?
Answer
Changes the state when the user clicks, before the event is raised
It is true by default; turn it off when the click must be refused.
-
Why does a form with three radio buttons and none checked compile and run?
Answer
The control does not require one to be checked; the form decides
The one-of-many rule applies to a click, not to the initial state.
-
Which property on a RadioButton is true for the chosen one?
Answer
Checked
RadioButton has Checked, as CheckBox does.
-
What does setting Checked = true on one radio button do to the others in its container?
Answer
Turns them off
Exactly one button of a container is on, so the others are cleared.
-
Why does one handler for two check boxes make the order of clicks irrelevant?
Answer
It reads the state of both every time instead of reacting to a delta
Reading the whole state at each change is the simplest way to stay consistent.
-
What kind of enumeration is FontStyle?
Answer
A [Flags] enumeration, combined with |
Bold and Italic are separate bits, so the two choices combine into one value.
-
Which control is right for a setting that is on or off independently of every other?
Answer
CheckBox
A radio button is for one choice among several; a check box stands alone.
Every listing on this page was built with the .NET SDK 10.0.303 for net10.0-windows against the targeting pack Microsoft.WindowsDesktop.App.Ref 10.0.15 from the offline feed, in a Docker container on Alpine Linux 3.24.3 (x86_64). The programs were built, not run on Linux: WinForms needs Windows.
Article author: Arthur Isaev