BinaryColorPickerComboBox
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
SelectedColorand raisesSelectedColorChanged; 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 | |
|---|---|
BinaryColorPickerComboBox | Initialises a colour picker with no colour selected; it shows a white swatch and the text "Empty". |
| Properties | |
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. |
AlphaBlendFactorForDropDownPressedColor | Gets or sets how strongly CustomPaintingColor shows in the drop-down button's pressed
look. |
AlphaBlendFactorForItemSelectionColor | Gets or sets how strongly CustomPaintingColor shows in the drop-down button and the
highlighted face. |
CustomColorRectangleBorderStyle | Gets or sets the 3-D edge drawn round each cell of the colour window's Custom palette. |
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. |
CustomPaintingColor | Gets or sets the colour the drop-down button and the highlighted face are shaded from. |
RightToLeft | Gets or sets right-to-left layout, which this control does not support: any value set is stored as
No. |
SelectedColor | Gets or sets the colour the control shows and the user last chose. |
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. |
ShowColorNameIfKnownColor | Gets or sets whether the control shows a colour's name – "Tomato", "ControlText" – instead of its RGB values when the colour has one. |
| Methods | |
Dispose | Releases the resources held by the control. |
DrawComboBoxFace | Draws the closed control: the background, the colour swatch and the text. |
DrawComboBoxFaceBackground | Draws the background of the closed control. Override to change it without taking over the swatch, the text and the drop-down button. |
DrawDisabledFace | Draws the control in its disabled form: an empty swatch outline and the text in
SystemColors.ControlDark. |
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 | Draws one row of the control's own item list: the background, the colour swatch and the text. |
DrawListItemBackground | 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. |
MeasureTextViaGdi | Measures a single line of text the way GDI draws it, which can differ from
Graphics.MeasureString. |
OnDrawItem | Draws an item of the control's own list with DrawListItem. The
ComboBox.DrawItem event is not raised. |
OnGotFocus | Raises the Control.GotFocus event and draws the border and the drop-down button in
their highlighted colours. |
OnHandleCreated | 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. |
OnKeyPress | Raises the Control.KeyPress event. |
OnLostFocus | Raises the Control.LostFocus event and, unless ShowBorderAlways is set,
takes the border away. |
OnMouseEnter | Raises the Control.MouseEnter event and draws the border and the drop-down button in
their highlighted colours. |
OnMouseLeave | Raises the Control.MouseLeave event and, unless the control has the focus or
ShowBorderAlways is set, takes the border away again. |
OnPaint | Raises the Control.Paint event and then draws the face, the border and the drop-down
button over the whole control. |
OnSelectedIndexChanged | Raises the ComboBox.SelectedIndexChanged event. |
SetFontHeight | Sets the control's Control.FontHeight, the line height its item layout is based on. |
WndProc | 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. |
| Events | |
SelectedColorChanged | Occurs when SelectedColor is set – by the user choosing a colour from the colour
window, or by code. |
| Fields | |
DropDownButtonWidth | The width, in logical pixels, of the shaded drop-down button drawn at the right-hand end of the control: 12. |
Constructors
BinaryColorPickerComboBox
Initialises a colour picker with no colour selected; it shows a white swatch and the text "Empty".
Properties
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
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
Gets or sets how strongly CustomPaintingColor shows in the drop-down button and the
highlighted face.
- Value
- From 0 (plain window colour) to 255 (
CustomPaintingColoritself). The default is 70.
Example
colourPicker.CustomPaintingColor = Color.SeaGreen;
colourPicker.AlphaBlendFactorForItemSelectionColor = 120; // a stronger green buttonCustomColorRectangleBorderStyle
Gets or sets the 3-D edge drawn round each cell of the colour window's Custom palette.
- Value
- One of the
Border3DStylevalues. The default isSunken.
Remarks
Applied each time the colour window opens.
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
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
Gets or sets right-to-left layout, which this control does not support: any value set is stored as
No.
- Value
- Always
Noonce set.
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
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
truefor a constant border; the default isfalse.
See also
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
trueto show names; the default isfalse.
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
Releases the resources held by the control.
Parameters
disposingtruewhen called fromDispose;falsewhen called from the finaliser.
DrawComboBoxFace
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.
isSelectedtrueto draw the face highlighted.hasFocustruewhen the control has the focus.
Remarks
The background is drawn by DrawComboBoxFaceBackground, which a derived class
can override.
DrawComboBoxFaceBackground
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.
isSelectedtrueto draw the face highlighted.hasFocustruewhen the control has the focus.
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
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
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.
isSelectedtruewhen the row is the selected one.hasFocustruewhen the row is the edit area of the combo box.
Remarks
The background is drawn by DrawListItemBackground, which a derived class can
override.
DrawListItemBackground
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.
isSelectedtruewhen the row is the selected one.hasFocustruewhen 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
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
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
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
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
Also sets Control.BackColor to SystemColors.Window.
OnKeyPress
Raises the Control.KeyPress event.
Parameters
e- The character typed.
Remarks
An exception thrown by a handler is caught and not passed on.
OnLostFocus
Raises the Control.LostFocus event and, unless ShowBorderAlways is set,
takes the border away.
Parameters
e- The data for the event.
OnMouseEnter
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
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
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
Raises the ComboBox.SelectedIndexChanged event.
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
Sets the control's Control.FontHeight, the line height its item layout is based on.
Parameters
s- The height, in pixels.
WndProc
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
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
The width, in logical pixels, of the shaded drop-down button drawn at the right-hand end of the control: 12.