Table of Contents

Class ListBox

Namespace
Alternet.UI
Assembly
Alternet.UI.dll

Represents a control to display a list of items.

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

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

ClearSelected()

Unselects all items in 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.

HitTest(PointD)

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

IsValidIndex(int)

Checks whether index is valid in the ListBox.

OnSelectionChanged(EventArgs)

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

RaiseSelectionChanged(EventArgs)

Raises the SelectionChanged event and calls OnSelectionChanged(EventArgs).

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.

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.