This Control wraps the Win32 listbox common
control, allowing you to display and select one or more items from a
list. It derives from the ListControl abstract
base class, in common with the ComboBox.
You can add or remove objects in the list by using the
Items collection. This could simply be a list of
strings, but could equally be any object that can be converted to a
string (which, in practice, means pretty much anything through the
ToString() member).
Alternatively, you can use data binding to bind to a data source. You
can independently set a DisplayMember and a
ValueMember.
In either case, the selected item can be retrieved through the
Text property (which returns the
DisplayMember) and
SelectedValue (which returns the
ValueMember), or you can retrieve its index with
the SelectedIndex property.
The SelectionMode allows you to specify the
various kinds of single- and multiple-selection modes available. If
multiple selection is enabled, you can use the
SelectedIndices and
SelectedItems collections to enumerate the entire
selection. You can select or deselect individual items using the
SetSelected() method.
If you need to locate a particular item in the list, you can use the
FindString() and FindStringExact() methods. FindString() identifies the
first item in the list that starts with the string specified, and is
therefore extremely useful for those match-as-you-type controls.
If the items added to the control are wider than the control itself,
you can enable a HorizontalScrollbar. The
HorizontalExtent property is then used to ensure
that the scrollbar width is set to the maximum width of an item for
the scrollbar to determine the appropriate range across which to
scroll.
You can also specify the ItemHeight in owner draw
scenarios. (See DrawItemEventArgs for more
information on owner draw).
public class ListBox : ListControl {
// Public Constructors
public ListBox();
// Public Static Fields
public const int DefaultItemHeight; // =13
public const int NoMatches; // =-1
// Public Instance Properties
public override Color BackColor{set; get; }
// overrides Control
public override Image BackgroundImage{set; get; }
// overrides Control
public BorderStyle BorderStyle{set; get; }
public int ColumnWidth{set; get; }
public virtual DrawMode DrawMode{set; get; }
public override Color ForeColor{set; get; }
// overrides Control
public int HorizontalExtent{set; get; }
public bool HorizontalScrollbar{set; get; }
public bool IntegralHeight{set; get; }
public virtual int ItemHeight{set; get; }
public ObjectCollection Items{get; }
public bool MultiColumn{set; get; }
public int PreferredHeight{get; }
public override RightToLeft RightToLeft{set; get; }
// overrides Control
public bool ScrollAlwaysVisible{set; get; }
public override int SelectedIndex{set; get; }
// overrides ListControl
public SelectedIndexCollection SelectedIndices{get; }
public object SelectedItem{set; get; }
public SelectedObjectCollection SelectedItems{get; }
public virtual SelectionMode SelectionMode{set; get; }
public bool Sorted{set; get; }
public override string Text{set; get; }
// overrides Control
public int TopIndex{set; get; }
public bool UseTabStops{set; get; }
// Protected Instance Properties
protected override CreateParams CreateParams{get; }
// overrides Control
protected override Size DefaultSize{get; }
// overrides Control
// Public Instance Methods
public void BeginUpdate();
public void ClearSelected();
public void EndUpdate();
public int FindString(string s);
public int FindString(string s, int startIndex);
public int FindStringExact(string s);
public int FindStringExact(string s, int startIndex);
public int GetItemHeight(int index);
public Rectangle GetItemRectangle(int index);
public bool GetSelected(int index);
public int IndexFromPoint(int x, int y);
public int IndexFromPoint(System.Drawing.Point p);
public void SetSelected(int index, bool value);
public override string ToString(); // overrides System.ComponentModel.Component
// Protected Instance Methods
protected virtual void AddItemsCore(object[ ] value);
protected virtual ObjectCollection CreateItemCollection();
protected override void OnChangeUICues(UICuesEventArgs e); // overrides Control
protected override void OnDataSourceChanged(EventArgs e); // overrides ListControl
protected override void OnDisplayMemberChanged(EventArgs e); // overrides ListControl
protected virtual void OnDrawItem(DrawItemEventArgs e);
protected override void OnFontChanged(EventArgs e); // overrides Control
protected override void OnHandleCreated(EventArgs e); // overrides Control
protected override void OnHandleDestroyed(EventArgs e); // overrides Control
protected virtual void OnMeasureItem(MeasureItemEventArgs e);
protected override void OnParentChanged(EventArgs e); // overrides Control
protected override void OnResize(EventArgs e); // overrides Control
protected override void OnSelectedIndexChanged(EventArgs e); // overrides ListControl
protected override void RefreshItem(int index); // overrides ListControl
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified); // overrides Control
protected override void SetItemCore(int index, object value); // overrides ListControl
protected override void SetItemsCore(System.Collections.IList value); // overrides ListControl
protected virtual void Sort();
protected virtual void WmReflectCommand(ref Message m);
protected override void WndProc(ref Message m); // overrides Control
// Events
public event EventHandler Click;
// overrides Control
public event DrawItemEventHandler DrawItem;
public event MeasureItemEventHandler MeasureItem;
public event PaintEventHandler Paint;
// overrides Control
public event EventHandler SelectedIndexChanged;
}