HypernovaDesktop Components · docs
Binarymission
WinForms › Button Suite › ExtendedCheckBox

ExtendedCheckBox

class.NET 6 / 8 / 10.NET Framework 4.8Binarymission.WinForms.Controls.ButtonControls

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

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
ExtendedCheckBoxInitialises a new, unchecked check box with a black box and tick that turn to the system highlight colour under the pointer.
Properties
ControlBackColorGets or sets the colour that fills the inside of the box.
FlatStyleGets or sets the flat style of the control, which is always Flat.
NormalControlColorGets or sets the colour of the box outline and the tick while the pointer is away and the control does not have the focus.
OnFocusControlColorGets 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
GetPreferredSizeCalculates the size that fits the box, the gap beside it and the text on one line.
OnGotFocusRaises the Control.GotFocus event and redraws the box in OnFocusControlColor.
OnLostFocusRaises the Control.LostFocus event and redraws the box in its normal colour.
OnMouseEnterRaises the Control.MouseEnter event and redraws the box in OnFocusControlColor.
OnMouseLeaveRaises the Control.MouseLeave event and redraws the box in its normal colour.
OnPaintDraws the control: the background, the box and its tick, the text, and the focus rectangle.
WndProcProcesses Windows messages exactly as the base control does.

Constructors

ExtendedCheckBox

public 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

public Color 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

public new FlatStyle 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

public Color 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

public Color 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

public override Size GetPreferredSize(Size proposedSize)

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

protected override void OnGotFocus(EventArgs e)

Raises the Control.GotFocus event and redraws the box in OnFocusControlColor.

Parameters
e
The event data.

OnLostFocus

protected override void OnLostFocus(EventArgs e)

Raises the Control.LostFocus event and redraws the box in its normal colour.

Parameters
e
The event data.

OnMouseEnter

protected override void OnMouseEnter(EventArgs e)

Raises the Control.MouseEnter event and redraws the box in OnFocusControlColor.

Parameters
e
The event data.

OnMouseLeave

protected override void OnMouseLeave(EventArgs e)

Raises the Control.MouseLeave event and redraws the box in its normal colour.

Parameters
e
The event data.

OnPaint

protected override void OnPaint(PaintEventArgs e)

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

protected override void WndProc(ref Message m)

Processes Windows messages exactly as the base control does.

Parameters
m
The message to process.