ExtendedCheckBox
A check box that draws its own box, tick and text, in colours you choose – a crisp, flat check box that looks the same on every version of Windows and at every display scale.
public sealed class ExtendedCheckBox : CheckBox
Inherits from / implements: CheckBox
Remarks
It is a CheckBox in every other way: CheckBox.Checked,
CheckBox.CheckState, CheckBox.ThreeState, the
CheckBox.CheckedChanged event, the keyboard and the accessibility all work as usual.
Only the picture changes.
Colours
NormalControlColordraws the box and the tick, black by default.OnFocusControlColortakes over while the pointer is over the control or it has the keyboard focus, the system highlight colour by default – so the control answers the pointer.ControlBackColorfills the inside of the box, the window colour by default.- The control's own
Control.BackColorfills the area round the box and the text, andControl.ForeColorcolours the text.
A disabled check box is drawn with a grey box on the system control colour and grey text.
Size and layout
The box grows with the font (to about four fifths of the font's height, and never below 11
logical pixels), and stays sharp at any display scale because it is drawn, not copied from an image.
CheckBox.CheckAlign, TextAlign,
Control.Padding and Control.RightToLeft place the box and the text as on a
standard check box.
An indeterminate check box (Indeterminate) is drawn with the same tick
as a checked one.
Examples
Two options on an export dialog, in the application's brand colours.
using System;
using System.Drawing;
using System.Windows.Forms;
using Binarymission.WinForms.Controls.ButtonControls;
public class ExportOptionsForm : Form
{
private readonly ExtendedCheckBox _includeInvoices = new ExtendedCheckBox();
private readonly ExtendedCheckBox _zipFiles = new ExtendedCheckBox();
public ExportOptionsForm()
{
Text = "Export orders";
_includeInvoices.Text = "Include invoices (PDF)";
_includeInvoices.Location = new Point(16, 16);
_includeInvoices.AutoSize = true;
_includeInvoices.Checked = true;
_includeInvoices.NormalControlColor = Color.FromArgb(0, 84, 147);
_includeInvoices.OnFocusControlColor = Color.FromArgb(0, 120, 212);
_includeInvoices.ControlBackColor = Color.AliceBlue;
_zipFiles.Text = "Compress into a single .zip file";
_zipFiles.Location = new Point(16, 44);
_zipFiles.AutoSize = true;
_zipFiles.NormalControlColor = Color.FromArgb(0, 84, 147);
_zipFiles.OnFocusControlColor = Color.FromArgb(0, 120, 212);
_zipFiles.CheckedChanged += (sender, e) => Console.WriteLine("Zip: " + _zipFiles.Checked);
Controls.Add(_includeInvoices);
Controls.Add(_zipFiles);
}
}See also
Members at a glance
| Constructors | |
|---|---|
ExtendedCheckBox | Initialises a new, unchecked check box with a black box and tick that turn to the system highlight colour under the pointer. |
| Properties | |
ControlBackColor | Gets or sets the colour that fills the inside of the box. |
FlatStyle | Gets or sets the flat style of the control, which is always Flat. |
NormalControlColor | Gets or sets the colour of the box outline and the tick while the pointer is away and the control does not have the focus. |
OnFocusControlColor | Gets or sets the colour of the box outline and the tick while the pointer is over the control or it has the keyboard focus. |
| Methods | |
GetPreferredSize | Calculates the size that fits the box, the gap beside it and the text on one line. |
OnGotFocus | Raises the Control.GotFocus event and redraws the box in OnFocusControlColor. |
OnLostFocus | Raises the Control.LostFocus event and redraws the box in its normal colour. |
OnMouseEnter | Raises the Control.MouseEnter event and redraws the box in OnFocusControlColor. |
OnMouseLeave | Raises the Control.MouseLeave event and redraws the box in its normal colour. |
OnPaint | Draws the control: the background, the box and its tick, the text, and the focus rectangle. |
WndProc | Processes Windows messages exactly as the base control does. |
Constructors
ExtendedCheckBox
Initialises a new, unchecked check box with a black box and tick that turn to the system highlight colour under the pointer.
Properties
ControlBackColor
Gets or sets the colour that fills the inside of the box.
- Value
- The fill colour of the box. The default is
SystemColors.Window.
Remarks
It fills the box only; the area round the box and the text is the control's
Control.BackColor. A fully transparent colour gives the window colour, and a disabled
check box always uses the system control colour.
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.
NormalControlColor
Gets or sets the colour of the box outline and the tick while the pointer is away and the control does not have the focus.
- Value
- The box and tick colour at rest. The default is
Color.Black.
Remarks
While the pointer is over the control or it has the focus, OnFocusControlColor
is used instead; a disabled control uses the system's dark grey.
See also
OnFocusControlColor
Gets or sets the colour of the box outline and the tick while the pointer is over the control or it has the keyboard focus.
- Value
- The highlighted box and tick colour. The default is
SystemColors.Highlight.
Remarks
The control returns to NormalControlColor when the pointer leaves it or it loses
the focus, whichever happens first.
See also
Methods
GetPreferredSize
Calculates the size that fits the box, 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 box, which is sized from the font.
OnGotFocus
Raises the Control.GotFocus event and redraws the box in OnFocusControlColor.
Parameters
e- The event data.
OnLostFocus
Raises the Control.LostFocus event and redraws the box in its normal colour.
Parameters
e- The event data.
OnMouseEnter
Raises the Control.MouseEnter event and redraws the box in OnFocusControlColor.
Parameters
e- The event data.
OnMouseLeave
Raises the Control.MouseLeave event and redraws the box in its normal colour.
Parameters
e- The event data.
OnPaint
Draws the control: the background, the box and its tick, 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.