HypernovaDesktop Components · docs
Binarymission
WinForms › Color Picker › BinaryColorPickerComboBox

BinaryColorPickerComboBox

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

A combo box for choosing a colour: it shows the chosen colour as a swatch with its RGB values or name, and opens a three-tab colour window – a custom palette, the web colours and the Windows system colours – in place of a list.

public class BinaryColorPickerComboBox : System.Windows.Forms.ComboBox

Inherits from / implements: System.Windows.Forms.ComboBox

Remarks

What it is for

Use it on settings and design forms wherever the user picks a colour – a chart series, a calendar category, a highlight for overdue invoices – and a full colour dialog is too much. It looks and tabs like a drop-down list, in the style of the Visual Studio colour picker.

How it works

  • A click anywhere on the control opens the colour window below it (a BinaryColorPickerDropDown). It has three tabs: Custom, an 8 x 8 palette whose two bottom rows the user can redefine by right-clicking a cell; Web, the named web colours; and System, the Windows system colours.
  • Choosing a colour sets SelectedColor and raises SelectedColorChanged; picking a palette cell or a list entry also closes the window. A click outside the window closes it with no change.
  • The control shows the colour's red, green and blue values – "70, 130, 180" – or, with ShowColorNameIfKnownColor, its name when it has one.

Appearance

The control draws itself flat. Its border appears in CustomControlBorderColor while it has the focus or the pointer, or always with ShowBorderAlways; the drop-down button and the highlight are shades of CustomPaintingColor, mixed by the three AlphaBlendFactor properties. Right-to-left layout is not supported.

Examples

using System;
using System.Drawing;
using System.Windows.Forms;
using Binarymission.WinForms.Controls.ColorPickers;

public class ChartSettingsForm : Form
{
    private readonly BinaryColorPickerComboBox seriesColourPicker;
    private readonly Panel preview;

    public ChartSettingsForm()
    {
        Text = "Chart settings";
        ClientSize = new Size(320, 120);

        seriesColourPicker = new BinaryColorPickerComboBox
        {
            Location = new Point(12, 12),
            Width = 200,
            ShowColorNameIfKnownColor = true,
            CustomControlBorderColor = Color.SteelBlue,
            CustomPaintingColor = Color.SteelBlue
        };
        seriesColourPicker.SelectedColorChanged += SeriesColourPicker_SelectedColorChanged;

        preview = new Panel { Location = new Point(224, 12), Size = new Size(84, 84), BorderStyle = BorderStyle.FixedSingle };

        Controls.Add(seriesColourPicker);
        Controls.Add(preview);

        seriesColourPicker.SelectedColor = Color.SteelBlue;   // shows "SteelBlue"
    }

    private void SeriesColourPicker_SelectedColorChanged(object sender, EventArgs e)
    {
        preview.BackColor = seriesColourPicker.SelectedColor;
    }
}

See also

Members at a glance

Constructors
BinaryColorPickerComboBoxInitialises a colour picker with no colour selected; it shows a white swatch and the text "Empty".
Properties
AlphaBlendFactorForControlPaintingGets or sets how strongly the window colour shows in the drop-down button's resting background, while the control has neither the focus nor the pointer.
AlphaBlendFactorForDropDownPressedColorGets or sets how strongly CustomPaintingColor shows in the drop-down button's pressed look.
AlphaBlendFactorForItemSelectionColorGets or sets how strongly CustomPaintingColor shows in the drop-down button and the highlighted face.
CustomColorRectangleBorderStyleGets or sets the 3-D edge drawn round each cell of the colour window's Custom palette.
CustomControlBorderColorGets or sets the colour of the control's border while it has the focus or the pointer is over it, or all the time with ShowBorderAlways.
CustomPaintingColorGets or sets the colour the drop-down button and the highlighted face are shaded from.
RightToLeftGets or sets right-to-left layout, which this control does not support: any value set is stored as No.
SelectedColorGets or sets the colour the control shows and the user last chose.
ShowBorderAlwaysGets or sets whether the border and the drop-down button are drawn in their highlighted colours all the time, rather than only while the control has the focus or the pointer.
ShowColorNameIfKnownColorGets or sets whether the control shows a colour's name – "Tomato", "ControlText" – instead of its RGB values when the colour has one.
Methods
DisposeReleases the resources held by the control.
DrawComboBoxFaceDraws the closed control: the background, the colour swatch and the text.
DrawComboBoxFaceBackgroundDraws the background of the closed control. Override to change it without taking over the swatch, the text and the drop-down button.
DrawDisabledFaceDraws the control in its disabled form: an empty swatch outline and the text in SystemColors.ControlDark.
DrawDisabledFaceBackgroundDraws the background, the grey border and the dimmed drop-down button of the disabled control. Override to change the disabled look without taking over the swatch and the text.
DrawListItemDraws one row of the control's own item list: the background, the colour swatch and the text.
DrawListItemBackgroundDraws the background of one row of the control's own item list. Override to change it without taking over the swatch and the text.
MeasureTextViaGdiMeasures a single line of text the way GDI draws it, which can differ from Graphics.MeasureString.
OnDrawItemDraws an item of the control's own list with DrawListItem. The ComboBox.DrawItem event is not raised.
OnGotFocusRaises the Control.GotFocus event and draws the border and the drop-down button in their highlighted colours.
OnHandleCreatedRemoves the Windows visual style from the new window, so the control's own drawing is not fought by the theme, and raises the Control.HandleCreated event.
OnKeyPressRaises the Control.KeyPress event.
OnLostFocusRaises the Control.LostFocus event and, unless ShowBorderAlways is set, takes the border away.
OnMouseEnterRaises the Control.MouseEnter event and draws the border and the drop-down button in their highlighted colours.
OnMouseLeaveRaises the Control.MouseLeave event and, unless the control has the focus or ShowBorderAlways is set, takes the border away again.
OnPaintRaises the Control.Paint event and then draws the face, the border and the drop-down button over the whole control.
OnSelectedIndexChangedRaises the ComboBox.SelectedIndexChanged event.
SetFontHeightSets the control's Control.FontHeight, the line height its item layout is based on.
WndProcProcesses the control's window messages: a click or double-click opens the colour window instead of the standard list, and each paint is followed by the control's own drawing.
Events
SelectedColorChangedOccurs when SelectedColor is set – by the user choosing a colour from the colour window, or by code.
Fields
DropDownButtonWidthThe width, in logical pixels, of the shaded drop-down button drawn at the right-hand end of the control: 12.

Constructors

BinaryColorPickerComboBox

public BinaryColorPickerComboBox()

Initialises a colour picker with no colour selected; it shows a white swatch and the text "Empty".

Properties

AlphaBlendFactorForControlPainting

public int AlphaBlendFactorForControlPainting

Gets or sets how strongly the window colour shows in the drop-down button's resting background, while the control has neither the focus nor the pointer.

Value
From 0 to 255. The default is 195.

AlphaBlendFactorForDropDownPressedColor

public int AlphaBlendFactorForDropDownPressedColor

Gets or sets how strongly CustomPaintingColor shows in the drop-down button's pressed look.

Value
From 0 (none of it) to 255 (all of it). The default is 70.
Remarks

The pressed look is drawn while ComboBox.DroppedDown is true. The colour window opens in place of the standard list and does not set ComboBox.DroppedDown, so in normal use this shade is not seen.

AlphaBlendFactorForItemSelectionColor

public int AlphaBlendFactorForItemSelectionColor

Gets or sets how strongly CustomPaintingColor shows in the drop-down button and the highlighted face.

Value
From 0 (plain window colour) to 255 (CustomPaintingColor itself). The default is 70.
Example
colourPicker.CustomPaintingColor = Color.SeaGreen;
colourPicker.AlphaBlendFactorForItemSelectionColor = 120;   // a stronger green button

CustomColorRectangleBorderStyle

public Border3DStyle CustomColorRectangleBorderStyle

Gets or sets the 3-D edge drawn round each cell of the colour window's Custom palette.

Value
One of the Border3DStyle values. The default is Sunken.
Remarks

Applied each time the colour window opens.

CustomControlBorderColor

public Color CustomControlBorderColor

Gets or sets the colour of the control's border while it has the focus or the pointer is over it, or all the time with ShowBorderAlways.

Value
The border colour. The default is SystemColors.Highlight.
Remarks

It also outlines the drop-down button and the highlighted face.

See also

CustomPaintingColor

public Color CustomPaintingColor

Gets or sets the colour the drop-down button and the highlighted face are shaded from.

Value
The base colour. The default is SystemColors.Highlight.
Remarks

The colour is not used as it is: it is mixed with the window colour by AlphaBlendFactorForItemSelectionColor for the button and the highlight, so the default gives a pale blue.

See also

RightToLeft

public override RightToLeft RightToLeft

Gets or sets right-to-left layout, which this control does not support: any value set is stored as No.

Value
Always No once set.

SelectedColor

public Color SelectedColor

Gets or sets the colour the control shows and the user last chose.

Value
The selected colour. The default is Color.Empty, shown as a white swatch with the text "Empty".
Remarks

Setting it updates the swatch and the text and, when the colour differs, raises SelectedColorChanged. The text shows the red, green and blue values; the alpha value is not shown. With ShowColorNameIfKnownColor a named colour shows its name instead.

The colour window opens on the tab for the colour last chosen in it, not for a colour set here in code.

Example
overdueColourPicker.SelectedColor = Color.FromArgb(220, 53, 69);   // shows "220, 53, 69"
Color chosen = overdueColourPicker.SelectedColor;

ShowBorderAlways

public bool ShowBorderAlways

Gets or sets whether the border and the drop-down button are drawn in their highlighted colours all the time, rather than only while the control has the focus or the pointer.

Value
true for a constant border; the default is false.
See also

ShowColorNameIfKnownColor

public bool ShowColorNameIfKnownColor

Gets or sets whether the control shows a colour's name – "Tomato", "ControlText" – instead of its RGB values when the colour has one.

Value
true to show names; the default is false.
Remarks

A colour has a name when Color.IsKnownColor is true: the colours on the Web and System tabs, and those set from the Color and SystemColors properties. Color.FromArgb(255, 99, 71) shows its values even though it matches Tomato. The change shows from the next time SelectedColor is set.

Methods

Dispose

protected override void Dispose(bool disposing)

Releases the resources held by the control.

Parameters
disposing
true when called from Dispose; false when called from the finaliser.

DrawComboBoxFace

protected void DrawComboBoxFace(Graphics g, Rectangle bounds, int index, bool isSelected, bool hasFocus)

Draws the closed control: the background, the colour swatch and the text.

Parameters
g
The surface to draw on.
bounds
The bounds of the control face, in client coordinates.
index
The index of the selected item.
isSelected
true to draw the face highlighted.
hasFocus
true when the control has the focus.
Remarks

The background is drawn by DrawComboBoxFaceBackground, which a derived class can override.

DrawComboBoxFaceBackground

protected virtual void DrawComboBoxFaceBackground(Graphics g, Rectangle bounds, int Index, bool isSelected, bool hasFocus)

Draws the background of the closed control. Override to change it without taking over the swatch, the text and the drop-down button.

Parameters
g
The surface to draw on.
bounds
The bounds of the control face; the background is drawn 3 pixels inside them.
Index
The index of the selected item.
isSelected
true to draw the face highlighted.
hasFocus
true when the control has the focus.

DrawDisabledFace

protected void DrawDisabledFace()

Draws the control in its disabled form: an empty swatch outline and the text in SystemColors.ControlDark.

Remarks

The background is drawn by DrawDisabledFaceBackground, which a derived class can override.

DrawDisabledFaceBackground

protected virtual void DrawDisabledFaceBackground()

Draws the background, the grey border and the dimmed drop-down button of the disabled control. Override to change the disabled look without taking over the swatch and the text.

DrawListItem

protected void DrawListItem(Graphics g, Rectangle bounds, int index, bool isSelected, bool hasFocus)

Draws one row of the control's own item list: the background, the colour swatch and the text.

Parameters
g
The surface to draw on.
bounds
The bounds of the row, in client coordinates.
index
The index of the item being drawn.
isSelected
true when the row is the selected one.
hasFocus
true when the row is the edit area of the combo box.
Remarks

The background is drawn by DrawListItemBackground, which a derived class can override.

DrawListItemBackground

protected virtual void DrawListItemBackground(Graphics g, Rectangle bounds, int Index, bool isSelected, bool hasFocus)

Draws the background of one row of the control's own item list. Override to change it without taking over the swatch and the text.

Parameters
g
The surface to draw on.
bounds
The bounds of the row.
Index
The index of the item being drawn.
isSelected
true when the row is the selected one.
hasFocus
true when the row is the edit area of the combo box.
Remarks

A selected row that is not the edit area is filled with the shade of CustomPaintingColor and outlined in CustomControlBorderColor; any other row is filled with the window colour.

MeasureTextViaGdi static

protected static Size MeasureTextViaGdi(Graphics graphics, string text, Font font)

Measures a single line of text the way GDI draws it, which can differ from Graphics.MeasureString.

Parameters
graphics
A graphics surface to measure against.
text
The text to measure.
font
The font it will be drawn in.
Returns

The width and height the text occupies, in pixels, or Size.Empty if it cannot be measured.

OnDrawItem

protected override void OnDrawItem(DrawItemEventArgs e)

Draws an item of the control's own list with DrawListItem. The ComboBox.DrawItem event is not raised.

Parameters
e
The item to draw and where.

OnGotFocus

protected override void OnGotFocus(EventArgs e)

Raises the Control.GotFocus event and draws the border and the drop-down button in their highlighted colours.

Parameters
e
The data for the event.

OnHandleCreated

protected override void OnHandleCreated(EventArgs e)

Removes the Windows visual style from the new window, so the control's own drawing is not fought by the theme, and raises the Control.HandleCreated event.

Parameters
e
The data for the event.
Remarks

OnKeyPress

protected override void OnKeyPress(KeyPressEventArgs e)

Raises the Control.KeyPress event.

Parameters
e
The character typed.
Remarks

An exception thrown by a handler is caught and not passed on.

OnLostFocus

protected override void OnLostFocus(EventArgs e)

Raises the Control.LostFocus event and, unless ShowBorderAlways is set, takes the border away.

Parameters
e
The data for the event.

OnMouseEnter

protected override void OnMouseEnter(EventArgs e)

Raises the Control.MouseEnter event and draws the border and the drop-down button in their highlighted colours.

Parameters
e
The data for the event.

OnMouseLeave

protected override void OnMouseLeave(EventArgs e)

Raises the Control.MouseLeave event and, unless the control has the focus or ShowBorderAlways is set, takes the border away again.

Parameters
e
The data for the event.

OnPaint

protected override void OnPaint(PaintEventArgs pe)

Raises the Control.Paint event and then draws the face, the border and the drop-down button over the whole control.

Parameters
pe
The graphics surface and the area to paint.

OnSelectedIndexChanged

protected override void OnSelectedIndexChanged(EventArgs e)
Parameters
e
The data for the event.
Remarks

The control's single item is replaced each time a colour is chosen, so the event follows each choice; SelectedColorChanged is the event to handle.

SetFontHeight

protected void SetFontHeight(int s)

Sets the control's Control.FontHeight, the line height its item layout is based on.

Parameters
s
The height, in pixels.

WndProc

protected override void WndProc(ref Message m)

Processes the control's window messages: a click or double-click opens the colour window instead of the standard list, and each paint is followed by the control's own drawing.

Parameters
m
The message to process.

Events

SelectedColorChanged

public event EventHandler SelectedColorChanged

Occurs when SelectedColor is set – by the user choosing a colour from the colour window, or by code.

Remarks

Raised only when the colour changes; assigning the colour already selected raises nothing. Read SelectedColor in the handler.

Fields

DropDownButtonWidth

protected const int DropDownButtonWidth

The width, in logical pixels, of the shaded drop-down button drawn at the right-hand end of the control: 12.