ExtendedRadioButton
A radio button that draws its own circle, dot and text, in colours you choose – a crisp, flat radio button that looks the same on every version of Windows and at every display scale.
public sealed class ExtendedRadioButton : System.Windows.Forms.RadioButton
Inherits from / implements: System.Windows.Forms.RadioButton
Remarks
It is a RadioButton in every other way: Checked, the
grouping of the buttons in the same container, CheckedChanged, the arrow
keys and the accessibility all work as usual. Only the picture changes.
Colours
NormalBorderColordraws the circle and the dot, black by default.OnFocusControlColortakes over while the pointer is over the button or it has the keyboard focus, the system highlight colour by default.ControlBackColorfills the inside of the circle, the window colour by default.- The control's own
Control.BackColorfills the area round the circle and the text, andControl.ForeColorcolours the text.
A disabled button is drawn with a grey circle and dot and grey text.
Size and layout
The circle grows with the font (to about four fifths of the font's height, and never below 11
logical pixels) and is drawn smoothed at every display scale. CheckAlign,
TextAlign, Control.Padding and Control.RightToLeft
place the circle and the text as on a standard radio button.
Examples
A choice of delivery speed, grouped in a GroupBox so only one can be chosen.
using System;
using System.Drawing;
using System.Windows.Forms;
using Binarymission.WinForms.Controls.ButtonControls;
// In the checkout form's constructor:
GroupBox delivery = new GroupBox { Text = "Delivery", Location = new Point(16, 16), Size = new Size(240, 110) };
string[] options = { "Standard (3-5 days)", "Express (next day)", "Collect from store" };
for (int i = 0; i < options.Length; i++)
{
ExtendedRadioButton option = new ExtendedRadioButton
{
Text = options[i],
Location = new Point(12, 24 + i * 26),
AutoSize = true,
Checked = i == 0,
NormalBorderColor = Color.DarkSlateGray,
OnFocusControlColor = Color.DarkOrange
};
option.CheckedChanged += (sender, e) =>
{
ExtendedRadioButton chosen = (ExtendedRadioButton)sender;
if (chosen.Checked)
{
Console.WriteLine("Delivery: " + chosen.Text);
}
};
delivery.Controls.Add(option);
}
Controls.Add(delivery);See also
Members at a glance
| Constructors | |
|---|---|
ExtendedRadioButton | Initialises a new, unselected radio button with a black circle and dot that turn to the system highlight colour under the pointer. |
| Properties | |
ControlBackColor | Gets or sets the colour that fills the inside of the circle. |
FlatStyle | Gets or sets the flat style of the control, which is always Flat. |
NormalBorderColor | Gets or sets the colour of the circle and the dot while the pointer is away and the button does not have the focus. |
OnFocusControlColor | Gets or sets the colour of the circle and the dot while the pointer is over the button or it has the keyboard focus. |
| Methods | |
GetPreferredSize | Calculates the size that fits the circle, the gap beside it and the text on one line. |
OnGotFocus | Raises the Control.GotFocus event and redraws the circle in OnFocusControlColor. |
OnLostFocus | Raises the Control.LostFocus event and redraws the circle in its normal colour. |
OnMouseEnter | Raises the Control.MouseEnter event and redraws the circle in OnFocusControlColor. |
OnMouseLeave | Raises the Control.MouseLeave event and redraws the circle in its normal colour. |
OnPaint | Draws the control: the background, the circle and its dot, the text, and the focus rectangle. |
WndProc | Processes Windows messages exactly as the base control does. |
Constructors
ExtendedRadioButton
Initialises a new, unselected radio button with a black circle and dot that turn to the system highlight colour under the pointer.
Properties
ControlBackColor
Gets or sets the colour that fills the inside of the circle.
- Value
- The fill colour of the circle. The default is
SystemColors.Window.
Remarks
It fills the circle only, in every state, disabled included; the area round the circle and
the text is the control's Control.BackColor.
See also
FlatStyle
Gets or sets the flat style of the control, which is always Flat.
- Value
- Always
Flat; a value set is ignored.
Remarks
Hidden from the designer and IntelliSense: the control draws every part of itself, so the base control's styles do not apply.
NormalBorderColor
Gets or sets the colour of the circle and the dot while the pointer is away and the button does not have the focus.
- Value
- The circle and dot colour at rest. The default is
Color.Black.
Remarks
While the pointer is over the button or it has the focus, OnFocusControlColor
is used instead; a disabled button uses the system's dark grey. The check box's equivalent is
ExtendedCheckBox.NormalControlColor.
See also
OnFocusControlColor
Gets or sets the colour of the circle and the dot while the pointer is over the button or it has the keyboard focus.
- Value
- The highlighted circle and dot colour. The default is
SystemColors.Highlight.
Remarks
The button returns to NormalBorderColor when the pointer leaves it or it loses
the focus, whichever happens first.
See also
Methods
GetPreferredSize
Calculates the size that fits the circle, the gap beside it and the text on one line.
Parameters
proposedSize- The size the layout is offering. Not used: the result depends only on the
text, font and
Control.Padding.
Returns
The width and height, in pixels, that show the whole text without wrapping.
Remarks
This is what Control.AutoSize uses, so an auto-sized control grows with its
font and its circle, which is sized from the font.
OnGotFocus
Raises the Control.GotFocus event and redraws the circle in OnFocusControlColor.
Parameters
e- The event data.
OnLostFocus
Raises the Control.LostFocus event and redraws the circle in its normal colour.
Parameters
e- The event data.
OnMouseEnter
Raises the Control.MouseEnter event and redraws the circle in OnFocusControlColor.
Parameters
e- The event data.
OnMouseLeave
Raises the Control.MouseLeave event and redraws the circle in its normal colour.
Parameters
e- The event data.
OnPaint
Draws the control: the background, the circle and its dot, the text, and the focus rectangle.
Parameters
e- The graphics surface and clip rectangle to paint.
Remarks
While Appearance is Normal the control draws
itself completely and the Control.Paint event is not raised. With
Button, the base control draws the push-button look, raises
Control.Paint as usual, and this control's colours do not apply.
The background is the control's Control.BackColor; a fully transparent one takes
the colour of the nearest parent that has an opaque background.
WndProc
Processes Windows messages exactly as the base control does.
Parameters
m- The message to process.