BinaryImageComboBox
A pick-list combo box that shows an image beside each item – and can turn itself into a ready-made drive list or font list.
public partial class BinaryImageComboBox : BinaryComboBox
Inherits from / implements: BinaryComboBox
Remarks
What it is for
Use it where a picture tells the items apart faster than words: priorities, statuses, file types, payment
methods, flags. It is always a DropDownList: the user picks, and cannot type. For typing and a
multi-column drop-down use BinaryTextComboBox.
Images and text
Item n is drawn with image n, from one of two sources. Either set Imagelist to an
ImageList and fill Items with the text; or set Bitmaps to an array of
bitmaps and BitmapsAssociatedText to the matching text, which fills Items for you.
Setting either source chooses it (UseImageListForComboBox). An item with no image keeps an empty image
space, so every item's text starts in the same place.
Each image is fitted, keeping its proportions, into a box DesiredWidthForDrawingTheImages by
DesiredHeightForDrawingTheImages logical pixels, and framed in
ColorForDrawingRectangleAroundImage while DrawRectangleAroundImage is on. Disabled, the
control greys the selected item's image.
Drive and font lists
Set ComboListStyle to ListStyle.DriveList for the computer's drives with their
shell icons, reported through DriveSelectionChanged; or to ListStyle.FontList for the
installed font families, each optionally drawn in its own typeface (FontDisplayStyle).
Things to know
- In
ListStyle.Standardstyle a click opens the list only once an image source has been set; until then use the keyboard (Alt+Down or F4). - Colours, border and drop-down button are those of
BinaryComboBox, always drawn flat.
Examples
A log-level picker with the standard Windows icons.
var severityPicker = new BinaryImageComboBox
{
Location = new Point(12, 12),
Width = 200,
DesiredWidthForDrawingTheImages = 16,
DesiredHeightForDrawingTheImages = 16,
DrawRectangleAroundImage = false,
};
severityPicker.Bitmaps = new[]
{
SystemIcons.Information.ToBitmap(),
SystemIcons.Warning.ToBitmap(),
SystemIcons.Error.ToBitmap(),
};
severityPicker.BitmapsAssociatedText = new[] { "Information", "Warning", "Error" };
severityPicker.SelectedIndex = 0;
severityPicker.SelectedIndexChanged += (sender, e) => Text = "Log level: " + severityPicker.SelectedItem;
Controls.Add(severityPicker);See also
Members at a glance
| Constructors | |
|---|---|
BinaryImageComboBox | Initialises a new image combo box in ListStyle.Standard style, with no images and no items. |
| Properties | |
Bitmaps | Gets or sets the bitmaps the items' images are drawn from: bitmap n beside item n. |
BitmapsAssociatedText | Gets or sets the text of each item, in the same order as Bitmaps; setting it replaces the
Items of the list. |
ColorForDrawingRectangleAroundImage | Gets or sets the colour of the frame drawn round each item's image. |
ComboListStyle | Gets or sets what the control lists: your own images and text, the computer's drives, or the installed fonts. |
DesiredHeightForDrawingTheImages | Gets or sets the height of the space each item's image is drawn in. |
DesiredWidthForDrawingTheImages | Gets or sets the width of the space each item's image is drawn in. |
DrawRectangleAroundImage | Gets or sets whether a frame is drawn round each item's image. |
DropDownStyle | Gets the style of the control, which is always DropDownList: the user picks from the list and
cannot type. |
FontDisplayStyle | Gets or sets whether a font list draws each font's name in that font, or all of them in the control's own font. |
Imagelist | Gets or sets the image list the items' images are drawn from: image n beside item n. |
Items | Gets the items of the list – the text shown beside each image. |
UseImageListForComboBox | Gets or sets which image source the items are drawn with: Imagelist, or Bitmaps. |
| Methods | |
Dispose | Releases the resources the control uses. |
GetItemDisplayText | Returns the text shown for one item when the control is bound to a data source: the item's
DisplayMember value. |
OnDpiChangedAfterParent | Raises the Control.DpiChangedAfterParent event and sizes the rows again for the new display scale. |
OnFontChanged | Raises the Control.FontChanged event and sizes the rows again, so they stay tall enough for the text. |
OnHandleCreated | Prepares the new window, and sizes the rows for the images when DesiredHeightForDrawingTheImages has been set. |
OnKeyDown | Raises the Control.KeyDown event; for Alt+Down and Alt+Up it raises
DropDownAboutToBeShown first, and a handler that cancels stops the key there. |
OnKeyUp | Raises the Control.KeyUp event, first raising DropDownAboutToBeShown when the Alt key is
part of the key combination. |
OnMeasureItem | Sets the height of each row of a drive list to the system's small icon size plus two logical pixels, and raises the
ComboBox.MeasureItem event for it. |
OnSelectedIndexChanged | Raises the ComboBox.SelectedIndexChanged event; an exception thrown by a handler is absorbed rather
than propagated. |
PaintControlWithData | Draws one item – its image, the image's frame and its text – in the list or on the closed control, or a drive or font entry in those styles. |
RefreshPaint | Redraws the selected item – image and text – on the closed control. |
SetData | Draws the disabled control: the selected item with its image greyed and half transparent, and its frame and text in the system's dimmed colours. |
WndProc | Processes Windows messages, raising DropDownAboutToBeShown when the user clicks the control, and
keeping a click from opening the list while there is no image source. |
| Events | |
DriveSelectionChanged | Occurs when the user picks a different entry in a drive list, with the details of the drive picked. |
DropDownAboutToBeShown | Occurs when the user is about to open the list – by clicking the control, or pressing Alt+Down or Alt+Up – and lets the handler stop it. |
Constructors
BinaryImageComboBox
Initialises a new image combo box in ListStyle.Standard style, with no images and no items.
Remarks
The control is a DropDownList that draws from Imagelist
(UseImageListForComboBox is true); set Imagelist or Bitmaps
next.
Properties
Bitmaps
Gets or sets the bitmaps the items' images are drawn from: bitmap n beside item n.
- Value
- The bitmaps, or
null(the default).
Remarks
Setting it also sets UseImageListForComboBox to false. Set
BitmapsAssociatedText to the matching text, in the same order. Available in code only; in the designer use
Imagelist.
See also
BitmapsAssociatedText
Gets or sets the text of each item, in the same order as Bitmaps; setting it replaces the
Items of the list.
- Value
- The item text, or
null(the default).
Remarks
ColorForDrawingRectangleAroundImage
Gets or sets the colour of the frame drawn round each item's image.
- Value
- The frame colour. The default is
SystemColors.Highlight.
Remarks
Used while DrawRectangleAroundImage is true. The frame is one logical pixel wide, just
outside the image; the disabled control draws it in SystemColors.ControlDark.
ComboListStyle
Gets or sets what the control lists: your own images and text, the computer's drives, or the installed fonts.
- Value
- One of the
ListStylevalues. The default isListStyle.Standard. - Default
ListStyle.Standard
Remarks
ListStyle.DriveList replaces the items with an entry for the computer itself followed by every
drive, each with its shell icon and display name, selects the first, and raises DriveSelectionChanged
as the user picks.
ListStyle.FontList replaces the items with the name of every installed font family that has a
regular style, unsorted, and selects the first. Each name is drawn in the control's font or
in its own typeface, as FontDisplayStyle says.
Both set the control's font to Microsoft Sans Serif 9 point, and the list is built when the property is set,
so set it again to pick up a drive or font added since. Setting ListStyle.Standard afterwards clears the
items: refill them, or set BitmapsAssociatedText again.
Example
var fontPicker = new BinaryImageComboBox { Location = new Point(12, 12), Width = 240 };
fontPicker.ComboListStyle = ListStyle.FontList;
fontPicker.FontDisplayStyle = FontNameDisplayStyle.DisplayUsingFontFamilyName;
fontPicker.SelectedIndexChanged += (sender, e) =>
previewBox.Font = new Font(fontPicker.SelectedItem.ToString(), 12f);
Controls.Add(fontPicker);DesiredHeightForDrawingTheImages
Gets or sets the height of the space each item's image is drawn in.
- Value
- The height in logical pixels, scaled to the display. The default is 24. Negative values are ignored.
Remarks
Until this is set, the rows keep the height the combo box gives them for its font, and an image taller than a row is drawn smaller to fit. Setting it makes every row tall enough for an image of this height with two logical pixels above and below it, or for a line of text if that is taller, and the closed control grows with the rows.
Windows limits a combo box row to 255 device pixels; on a display scaled far enough for a row to reach that, the images are drawn smaller to fit, keeping their shape. The drive and font lists size their own rows.
DesiredWidthForDrawingTheImages
Gets or sets the width of the space each item's image is drawn in.
- Value
- The width in logical pixels, scaled to the display. The default is 20. Negative values are ignored.
Remarks
An image keeps its shape: it is scaled to fit the box this property and
DesiredHeightForDrawingTheImages describe, and centred in it. Every item keeps this space, with or
without an image, and the text starts after it.
DrawRectangleAroundImage
Gets or sets whether a frame is drawn round each item's image.
- Value
trueto frame the images. The default istrue.
Remarks
Turn it off for icons with transparent backgrounds, which look better unframed.
See also
DropDownStyle
Gets the style of the control, which is always DropDownList: the user picks from the list and
cannot type.
- Value
DropDownList. Setting any value setsDropDownList.
FontDisplayStyle
Gets or sets whether a font list draws each font's name in that font, or all of them in the control's own font.
- Value
- One of the
FontNameDisplayStylevalues. The default isFontNameDisplayStyle.DisplayUsingControlFont. - Default
FontNameDisplayStyle.DisplayUsingControlFont
Remarks
Applies in ListStyle.FontList style. Drawing each name in its own typeface is the more useful preview,
but symbol fonts then show symbols rather than a readable name.
Imagelist
Gets or sets the image list the items' images are drawn from: image n beside item n.
- Value
- The image list, or
null(the default).
Remarks
Setting it also sets UseImageListForComboBox to true. The text comes from
Items, which you fill yourself; an item beyond the last image is drawn without one.
The images are drawn at the size of the image box (DesiredWidthForDrawingTheImages by
DesiredHeightForDrawingTheImages), not at ImageList.ImageSize, so give the list an image
size at least that large for sharp pictures on high-DPI displays.
Example
var icons = new ImageList { ImageSize = new Size(32, 32), ColorDepth = ColorDepth.Depth32Bit };
icons.Images.Add(cardIcon);
icons.Images.Add(bankTransferIcon);
icons.Images.Add(invoiceIcon);
paymentMethodPicker.Imagelist = icons;
paymentMethodPicker.Items.AddRange(new object[] { "Card", "Bank transfer", "Invoice" });
paymentMethodPicker.SelectedIndex = 0;Items
Gets the items of the list – the text shown beside each image.
- Value
- The items. Item
nis drawn with imagenof the chosen image source.
Remarks
Setting BitmapsAssociatedText, or switching ComboListStyle, replaces the items. In the
designer the items are saved with the form.
UseImageListForComboBox
Remarks
Methods
Dispose
Releases the resources the control uses.
Parameters
disposingtruewhen called fromDispose().
Remarks
GetItemDisplayText
Returns the text shown for one item when the control is bound to a data source: the item's
DisplayMember value.
Parameters
g- Not used.
brush- Not used.
index- The zero-based index of the item.
Returns
The item's display text; the object's own ToString text when there is no display member;
or an empty string when there is no data source, the item does not exist, or (for a list of objects) the property is not
a string.
OnDpiChangedAfterParent
Raises the Control.DpiChangedAfterParent event and sizes the rows again for the new display scale.
Parameters
e- The event data.
OnFontChanged
Raises the Control.FontChanged event and sizes the rows again, so they stay tall enough for the text.
Parameters
e- The event data.
OnHandleCreated
Prepares the new window, and sizes the rows for the images when DesiredHeightForDrawingTheImages has been set.
Parameters
e- The event data.
OnKeyDown
Raises the Control.KeyDown event; for Alt+Down and Alt+Up it raises
DropDownAboutToBeShown first, and a handler that cancels stops the key there.
Parameters
e- The key pressed.
OnKeyUp
Raises the Control.KeyUp event, first raising DropDownAboutToBeShown when the Alt key is
part of the key combination.
Parameters
e- The key released.
OnMeasureItem
Sets the height of each row of a drive list to the system's small icon size plus two logical pixels, and raises the
ComboBox.MeasureItem event for it.
Parameters
e- The item being measured.
Remarks
In the other styles the event is not raised and the rows keep the height already set.
OnSelectedIndexChanged
Raises the ComboBox.SelectedIndexChanged event; an exception thrown by a handler is absorbed rather
than propagated.
Parameters
e- The event data.
PaintControlWithData
Draws one item – its image, the image's frame and its text – in the list or on the closed control, or a drive or font entry in those styles.
Parameters
g- The surface to draw on.
bounds- The item's bounds, in device pixels.
Index- The index of the item.
ifSelectedtruewhen the item is the highlighted one.toEdittruewhen drawing the closed control.
RefreshPaint
Redraws the selected item – image and text – on the closed control.
Parameters
g- The surface to draw on.
bounds- The control's face, in device pixels.
Index- The index of the selected item, or -1 for none.
ifSelectedtrueto draw the item highlighted.toEdittruewhen drawing the closed control.
SetData
Draws the disabled control: the selected item with its image greyed and half transparent, and its frame and text in the system's dimmed colours.
WndProc
Processes Windows messages, raising DropDownAboutToBeShown when the user clicks the control, and
keeping a click from opening the list while there is no image source.
Parameters
m- The message to process.
Events
DriveSelectionChanged
Occurs when the user picks a different entry in a drive list, with the details of the drive picked.
Remarks
Raised only in ListStyle.DriveList style. The first entry is the computer itself, whose
DriveInformation.DrivePathString is empty; check it before using the path.
Example
drivePicker.ComboListStyle = ListStyle.DriveList;
drivePicker.DriveSelectionChanged += (sender, drive) =>
{
if (drive.DrivePathString.Length == 0)
return; // the "This PC" entry
folderList.Items.Clear();
folderList.Items.AddRange(Directory.GetDirectories(drive.DrivePathString));
Text = drive.DriveDisplayName + " - " + drive.DriveTypeString;
};DropDownAboutToBeShown
Occurs when the user is about to open the list – by clicking the control, or pressing Alt+Down or Alt+Up – and lets the handler stop it.
Remarks
Set the handler's cancelNow argument to true to keep the list closed. The event can be raised
more than once for one gesture, so keep the handler free of side effects such as message boxes.
Example
paymentMethodPicker.DropDownAboutToBeShown += (object sender, ref bool cancelNow) =>
{
cancelNow = orderIsLocked; // the payment method cannot change once the order is invoiced
};