Table of Contents

Class ListBox

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

Represents a control that displays a list of items and allows the user to select one or more items. This control encapsulates the native list box implemented by the operating system. It has limited functionality. For the full featured list control, use the VirtualListBox or StdListBox controls.

[DefaultBindingProperty("SelectedValue")]
[ControlCategory("Common")]
public class ListBox : Control, IBaseObjectWithAttr, IBaseObjectWithId, INotifyPropertyChanged, IDisposableObject, IBaseObject, ISupportInitialize, IFocusable, ITextProperty, IComponent, IControl, IDisposable, IWin32Window, ILayoutItem, INotifyDataErrorInfo, ICollectionChangeRouter, IListBoxActions
Inheritance
ListBox
Implements
Derived

Examples

Here is how to declare a ListBox in UIXML:

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

And the corresponding event handler in the code-behind:

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

Also, a ListBox can be created from code:

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

Remarks

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.

ListBox(Control)

Initializes a new instance of the ListBox class with the specified parent control.

Properties

CheckBoxVisible

Gets whether checkboxes are shown in the items.

HasBorder

Gets or sets a value indicating whether the list box has a visible border.

HorizontalScrollbar

Indicates whether or not the list box should display a horizontal scrollbar when the items extend beyond the right edge of the list box. If true, the scrollbar will automatically set its extent depending on the length of items in the list box. This property may not be supported on some platforms. Default is false.

IntegralHeight

Indicates if the list box should avoid showing partial items. If so, then only full items will be displayed, and the list box will be resized to prevent partial items from being shown. Otherwise, they will be shown. Default is true.

IsSelectionModeMultiple

Gets or sets whether selection mode is multiple.

IsSelectionModeSingle

Gets or sets whether selection mode is single.

Items

Gets the items of the ListBox.

ScrollAlwaysVisible

Gets or sets whether the vertical scrollbar is shown at all times. Default is false, which means the scrollbar is shown only when needed.

SelectedIndex

The index of the currently selected item in the list, if there is one. If the value is -1, there is currently no selection. If the value is 0 or greater, than the value is the index of the currently selected item. selection.

SelectedIndices

A collection of the indices of the selected items in the list box. If there are no selected items in the list box, the result is an empty collection.

SelectedItem

The value of the currently selected item in the list, if there is one. If the value is null, there is currently no selection. If the value is non-null, then the value is that of the currently selected item. If the MultiSelect property on the list box is true, then a non-null return value for this method is the value of the first item selected.

SelectedItems

The collection of selected items.

SelectionMode

Controls how many items at a time can be selected in the list box. Valid values are from the SelectionMode enumeration.

Text

Gets or sets the text associated with this control.

TopIndex

The index of the first visible item in a list box. Initially the item with index 0 is at the top of the list box, but if the list box contents have been scrolled another item may be at the top.

Methods

ClearSelected()

Deselects all currently selected items.

CreateHandler()

Creates a handler for the control.

CreateItemCollection()

Creates a new instance of the item collection.

Deselect(int)

Deselects the item at the specified index.

EnsureVisible(int)

Ensures that the item at the given index is visible in the viewport.

FindStringExact(string, bool)

Finds the index of the first item whose string representation matches the specified text.

GetCount()

Gets the number of items in the list box.

GetCountPerPage()

Gets the number of items that are visible per page in the list box viewport.

GetItemText(object?)

Converts the specified item to its string representation.

GetSelected(int)

Determines whether the item at the specified index is selected.

GetSelection()

Gets the index of the current selection.

HitTest(PointD)

Performs a hit test at the specified point and returns the index of the item under that point.

IsSorted()

Determines whether the items in the list box are displayed in sorted order.

OnInsertedToParent(AbstractControl)

Called when this control is inserted into a parent control.

OnSelectedIndexChanged(EventArgs)

Called when the SelectedIndex property or the SelectedIndices property changes.

RaiseSelectedIndexChanged()

Raises the SelectedIndexChanged event and OnSelectedIndexChanged(EventArgs) method.

RemoveAll()

Removes all items from the list box.

RemoveSelectedItems()

Removes the items at the currently selected indices from the collection.

SelectAll()

Selects all items in the list box, if the selection mode allows multiple selections.

SelectFirstItem()

Selects the first item in the list box, if any items are available.

SelectLastItem()

Selects the last item in the list box, if any items are available.

SetSelected(int, bool)

Updates the selection state of the specified item.

SetSelection(int)

Sets the selection to the item at the specified index.

SetStringSelection(string, bool)

Selects or deselects the first item whose content matches the specified string.

ToString()

Returns a string representation for this control.

UnselectAll()

Deselects all currently selected items.

Events

SelectedIndexChanged

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

TextChanged

This event is not relevant for this control and is hidden from intellisense.