Table of Contents

Class ListBox

Namespace
Alternet.UI
Assembly
Alternet.UI.Common.dll

Represents a control to display a list of items. Please consider using VirtualListBox instead of this simple control.

[ControlCategory("Common")]
public class ListBox : ListControl, IBaseObject, IDisposableObject, ISupportInitialize, IFocusable, ITextProperty, IComponent, IControl, IDisposable, IWin32Window, INotifyDataErrorInfo, IReadOnlyStrings
Inheritance
ListBox
Implements
Derived

Examples

Here is how to declare a ListBox in UIXML:

<ListBox Margin="8" Name="listBox" SelectionChanged="ListBox_SelectionChanged"/>

And the corresponding event handler in the code-behind:

private void ListBox_SelectionChanged(object? sender, EventArgs e)
{
    Application.Log("ListBox: SelectionChanged. SelectedIndex: " + listBox.SelectedIndex.ToString());
}

Also, a ListBox can be created from code:

var ListBox = new Alternet.UI.ListBox();
ListBox.Items.Add("One");
ListBox.Items.Add("Two");
ListBox.Items.Add("Three");
ListBox.SelectedIndex = 1;

Remarks

The ListBox control enables you to display a list of items to the user that the user can select by clicking. A ListBox control can provide single or multiple selections using the SelectionMode property. The BeginUpdate() and EndUpdate() methods enable you to add a large number of items to the ListBox without the control being repainted each time an item is added to the list. The Items, SelectedItems, and SelectedIndices properties provide access to the three collections that are used by the ListBox.

Examples of how a ListBox can look on different platforms:

A ListBox, like any other Control, can be disabled by setting its Enabled property to false.

Constructors

ListBox()

Initializes a new instance of the ListBox class.

Properties

ControlKind

Returns control identifier.

HasBorder

Gets or sets a value indicating whether the control has a border.

SelectedIndex

Gets or sets the zero-based index of the currently selected item in a ListBox.

SelectedIndexes

Same as SelectedIndices

SelectedIndices

Gets a collection that contains the zero-based indexes of all currently selected items in the ListBox.

SelectedIndicesDescending

Gets a collection that contains the zero-based indexes of all currently selected items in the ListBox.

SelectedItem

Gets or sets the currently selected item in the ListBox.

SelectedItems

Gets a collection containing the currently selected items in the ListBox.

SelectionMode

Gets or sets the method in which items are selected in the ListBox.

Methods

CanRemoveSelectedItem()

Gets whether selected item can be removed.

ClearSelected()

Unselects all items in the control.

CreateHandler()

Creates a handler for the control.

EnsureVisible(int)

Ensures that the item is visible within the control, scrolling the contents of the control, if necessary.

GetValidIndexes(params int[])

Gets only valid indexes from the list of indexes in the ListBox.

HasItems()

Gets whether control has items.

HasSelectedItems()

Gets whether control has selected items.

HitTest(PointD)

Returns the zero-based index of the item at the specified coordinates.

IsValidIndex(int)

Checks whether index is valid in the ListBox.

OnMouseDoubleClick(MouseEventArgs)

Called when the mouse button was double-clicked.

OnSelectionChanged(EventArgs)

Called when the SelectedIndex property or the SelectedIndices collection has changed.

RaiseSelectionChanged(EventArgs)

Raises the SelectionChanged event and calls OnSelectionChanged(EventArgs).

RemoveSelectedItem()

Removes selected item from the control.

RemoveSelectedItems()

Removes selected items from the ListBox.

SelectItems(params int[])

Selects items with specified indexes in the ListBox.

SelectedItemsAsText(string?)

Gets selected items as string.

SelectedItemsToClipboard(string?)

Copies result of the SelectedItemsAsText(string?) to clipboard.

SetSelected(int, bool)

Selects or clears the selection for the specified item in a ListBox.

Events

SelectionChanged

Occurs when the SelectedIndex property or the SelectedIndices collection has changed.

SelectionModeChanged

Occurs when the value of the SelectionMode property changes.