HypernovaDesktop Components · docs
Binarymission
WinForms › Combo Box › BinaryImageComboBox

BinaryImageComboBox

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

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.Standard style 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
BinaryImageComboBoxInitialises a new image combo box in ListStyle.Standard style, with no images and no items.
Properties
BitmapsGets or sets the bitmaps the items' images are drawn from: bitmap n beside item n.
BitmapsAssociatedTextGets or sets the text of each item, in the same order as Bitmaps; setting it replaces the Items of the list.
ColorForDrawingRectangleAroundImageGets or sets the colour of the frame drawn round each item's image.
ComboListStyleGets or sets what the control lists: your own images and text, the computer's drives, or the installed fonts.
DesiredHeightForDrawingTheImagesGets or sets the height of the space each item's image is drawn in.
DesiredWidthForDrawingTheImagesGets or sets the width of the space each item's image is drawn in.
DrawRectangleAroundImageGets or sets whether a frame is drawn round each item's image.
DropDownStyleGets the style of the control, which is always DropDownList: the user picks from the list and cannot type.
FontDisplayStyleGets or sets whether a font list draws each font's name in that font, or all of them in the control's own font.
ImagelistGets or sets the image list the items' images are drawn from: image n beside item n.
ItemsGets the items of the list – the text shown beside each image.
UseImageListForComboBoxGets or sets which image source the items are drawn with: Imagelist, or Bitmaps.
Methods
DisposeReleases the resources the control uses.
GetItemDisplayTextReturns the text shown for one item when the control is bound to a data source: the item's DisplayMember value.
OnDpiChangedAfterParentRaises the Control.DpiChangedAfterParent event and sizes the rows again for the new display scale.
OnFontChangedRaises the Control.FontChanged event and sizes the rows again, so they stay tall enough for the text.
OnHandleCreatedPrepares the new window, and sizes the rows for the images when DesiredHeightForDrawingTheImages has been set.
OnKeyDownRaises the Control.KeyDown event; for Alt+Down and Alt+Up it raises DropDownAboutToBeShown first, and a handler that cancels stops the key there.
OnKeyUpRaises the Control.KeyUp event, first raising DropDownAboutToBeShown when the Alt key is part of the key combination.
OnMeasureItemSets 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.
OnSelectedIndexChangedRaises the ComboBox.SelectedIndexChanged event; an exception thrown by a handler is absorbed rather than propagated.
PaintControlWithDataDraws 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.
RefreshPaintRedraws the selected item – image and text – on the closed control.
SetDataDraws the disabled control: the selected item with its image greyed and half transparent, and its frame and text in the system's dimmed colours.
WndProcProcesses 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
DriveSelectionChangedOccurs when the user picks a different entry in a drive list, with the details of the drive picked.
DropDownAboutToBeShownOccurs 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

public 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

public Bitmap[] 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

public string[] 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

Setting an array clears Items and adds one item for each string. Setting null leaves Items as they are. Available in code only.

ColorForDrawingRectangleAroundImage

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

public ListStyle 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 ListStyle values. The default is ListStyle.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

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

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

public bool DrawRectangleAroundImage

Gets or sets whether a frame is drawn round each item's image.

Value
true to frame the images. The default is true.
Remarks

Turn it off for icons with transparent backgrounds, which look better unframed.

See also

DropDownStyle

public new ComboBoxStyle 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 sets DropDownList.

FontDisplayStyle

public FontNameDisplayStyle 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 FontNameDisplayStyle values. The default is FontNameDisplayStyle.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

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

public new ComboBox.ObjectCollection Items

Gets the items of the list – the text shown beside each image.

Value
The items. Item n is drawn with image n of the chosen image source.
Remarks

Setting BitmapsAssociatedText, or switching ComboListStyle, replaces the items. In the designer the items are saved with the form.

UseImageListForComboBox

public bool UseImageListForComboBox

Gets or sets which image source the items are drawn with: Imagelist, or Bitmaps.

Value
true to draw from Imagelist; false to draw from Bitmaps. The default is true.
Remarks

Setting Imagelist sets this to true, and setting Bitmaps sets it to false, so it rarely needs setting by hand. While the chosen source is empty, a click does not open the list.

Methods

Dispose

protected override void Dispose(bool disposing)

Releases the resources the control uses.

Parameters
disposing
true when called from Dispose().
Remarks

The images in Imagelist and Bitmaps belong to the application and are not disposed.

GetItemDisplayText

protected string GetItemDisplayText(Graphics g, Brush brush, int index)

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

protected override void OnDpiChangedAfterParent(EventArgs e)

Raises the Control.DpiChangedAfterParent event and sizes the rows again for the new display scale.

Parameters
e
The event data.

OnFontChanged

protected override void OnFontChanged(EventArgs e)

Raises the Control.FontChanged event and sizes the rows again, so they stay tall enough for the text.

Parameters
e
The event data.

OnHandleCreated

protected override void OnHandleCreated(EventArgs e)

Prepares the new window, and sizes the rows for the images when DesiredHeightForDrawingTheImages has been set.

Parameters
e
The event data.

OnKeyDown

protected override void OnKeyDown(KeyEventArgs e)

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

protected override void OnKeyUp(KeyEventArgs e)

Raises the Control.KeyUp event, first raising DropDownAboutToBeShown when the Alt key is part of the key combination.

Parameters
e
The key released.

OnMeasureItem

protected override void OnMeasureItem(MeasureItemEventArgs e)

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

protected override void OnSelectedIndexChanged(EventArgs e)

Raises the ComboBox.SelectedIndexChanged event; an exception thrown by a handler is absorbed rather than propagated.

Parameters
e
The event data.

PaintControlWithData

protected override void PaintControlWithData(Graphics g, Rectangle bounds, int Index, bool ifSelected, bool toEdit)

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.
ifSelected
true when the item is the highlighted one.
toEdit
true when drawing the closed control.

RefreshPaint

protected override void RefreshPaint(Graphics g, Rectangle bounds, int Index, bool ifSelected, bool toEdit)

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.
ifSelected
true to draw the item highlighted.
toEdit
true when drawing the closed control.

SetData

protected override void 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

protected override void WndProc(ref System.Windows.Forms.Message m)

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

public event DriveSelectionChangeHandler 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

public event DropDownProcessHandler 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
};