Introduction
While working on a project with an Office 2003 look, I needed a color picker with the exact look and feel of a Windows 2003 application. So, I've created one.
Using the code
The project has three controls that are available for use: OfficeColorPicker
, ComboBoxColorPicker
and ToolStripColorPicker
. The first two controls are ToolBoxItem
controls and therefore using them is as simple as dragging the control from the toolbox to your form. The third control is derived from ToolStripDropDownButton
and will be discussed later in this article. All of these controls have a Color
property to get or set the selected color of the control. Also, they have an event called SelectedColorChanged
that occurs when the selected color of the control changes.
OfficeColorPicker
This control holds all of the colors and functionality for color picking. It can be used modeless in the application by this code, where this
is a Form
or any other container:
OfficeColorPicker officeColorPicker = new OfficeColorPicker();
this.Controls.Add(officeColorPicker);
Of course, the better way to open the color picker is as a pop-up, using the other two controls provided.
ComboBoxColorPicker
This control derives from System.Windows.Forms.ComboBox
to show the selected color in the ComboBox
. When clicking on the drop-down arrow, it will open the OfficeColorPicker
control in a pop-up with context menu behavior, i.e. it will close on selection or on loss of focus.
To use this combo box control:
ComboBoxColorPicker comboBoxColorPicker =
new ComboBoxColorPicker();
this.Controls.Add(comboBoxColorPicker);
ToolStripColorPicker
This control derives from System.Windows.Forms.ToolStripDropDownButton
in order to allow using the button inside any xxxStrip
control. Examples include ToolStrip
, ContextMenuStrip
, MenuStrip
and any other controls that "know" to hold ToolStripItem
. To add ToolStripColorPicker
to ToolStrip
, use the following code:
ToolStrip toolStrip = new ToolStrip();
this.Controls.Add(toolStrip);
ToolStripColorPicker toolStripColorPicker =
new ToolStripColorPicker(Color.White);
toolStrip.Items.Add(toolStripColorPicker);
An easier way of using ToolStripColorPicker
is to employ the design-time support of VS 2005. Just add ToolStrip
or any other xxxStrip
control to the form and use the drop-down list of controls to add ToolStripColorPicker
:
VS 2005 uses some sort of reflection to find the ToolStripItem
available to add, but this subject is beyond the scope of this article. ToolStripColorPicker
has the following properties:
public ToolStripColorPickerDisplayType ButtonDisplayStyle;
public Color Color;
public bool AddColorNameToToolTip;
new public string ToolTipText;
The ButtonDisplayStyle
property, unlike the DisplayStyle
one, is used in order to specify whether to paint the image, the text and the underline for the button instead of just the image and the text, as in DisplayStyle
.
Events
As mentioned above, all of these controls have the SelectedColorChanged
event. This event occurs when the selected color of the control changes. To use this event:
...
this.colorPickerControl.SelectedColorChanged +=
new System.EventHandler(colorPickerControl_SelectedColorChanged);
...
private void (colorPickerControl_SelectedColorChanged);
(object sender, EventArgs e)
{
Color newColor = colorPickerControl.Color;
...
}
Points of interest
To allow context menu behavior, I've created a ContextMenuForm
class that derives from System.Windows.Forms.Form
. This form may hold any Control
instance and will hide itself when it loses focus. To use this form:
OfficePickers.Util.ContextMenuForm contextMenuForm =
new OfficePickers.Util.ContextMenuForm();
contextMenuForm.SetContainingControl(control);
contextMenuForm.Show(this, new Point(0, 0), 100);
You can use this form for any control you wish. To close the form -- i.e. the user selects an option -- use the following code in your control:
Form parentForm = this.FindForm();
if(parentForm != null)
{
parentForm.Hide();
}
Improvements
- Add support for XP themes: to do this, you should implement a Theme reader and interact it with the
CustomColors
class that holds all the colors.
- Add the automatic color button: just paint another button like the More Colors button and implement the selection for it.
History
- 8.12.2005 - Version 1.0 of
OfficeColorPicker
posted.
- 1.8.2007 - Version 1.1 released, including:
- Focus bug fixed.
- When changing Enable to false, the button is painted in grayscale.
- Support for
SplitButton
: clicking on the arrow will display the color selection as today, but clicking on the button portion itself will fire SelectedColorChanged
to use for changing the color of the current selected element, like in Office.