Skip to content

ListBox Class

A scrollable list control.

C#
[DataContract]
public class ListBox : CompositeControl, IContainer, IList<ControlBase>, ICollection<ControlBase>, IEnumerable<ControlBase>, IEnumerable

Inheritance objectControlBaseCompositeControl

Implements IContainer, IList<ControlBase>, ICollection<ControlBase>, IEnumerable<ControlBase>, IEnumerable

Creates a new instance of the listbox control with the default theme for the items.

C#
public ListBox(int width, int height)

width int
The width of the listbox.

height int
The height of the listbox.

Creates a new instance of the listbox control with the specified item theme.

C#
public ListBox(int width, int height, ListBoxItemTheme itemTheme)

width int
The width of the listbox.

height int
The height of the listbox.

itemTheme ListBoxItemTheme
The theme to use with rendering the listbox items.

Internal flag to indicate the scroll bar needs to be reconfigured.

C#
protected bool _reconfigureScrollBar

Internal use only; used in rendering.

C#
public bool IsScrollBarVisible { get; set; }

The scroll bar control used with this list box.

C#
[DataMember(Name = "ScrollBar")]
public ScrollBar ScrollBar { get; protected set; }

Used in rendering.

C#
public int ItemIndexMouseOver { get; }

The total items visible in the listbox.

C#
public int VisibleItemsTotal { get; set; }

The maximum amount of items that can be shown in the listbox.

C#
public int VisibleItemsMax { get; set; }

When the SelectedItem changes, and this property is true, objects will be compared by reference. If false, they will be compared by value.

C#
[DataMember]
public bool CompareByReference { get; set; }

When set to true, the SelectedItemExecuted event will fire when an item is single-clicked instead of double-clicked.

C#
[DataMember]
public bool SingleClickItemExecute { get; set; }

The items in the listbox.

C#
[DataMember]
public ObservableCollection<object> Items { get; }

Gets or sets the index of the selected item.

C#
public int SelectedIndex { get; set; }

Gets or sets the selected item.

C#
public object? SelectedItem { get; set; }

The drawing theme for the border when DrawBorder is true.

C#
public ThemeStates BorderTheme { get; protected set; }

The line style for the border when DrawBorder is true.

C#
[DataMember]
public int[] BorderLineStyle { get; set; }

If false the border will not be drawn.

C#
[DataMember]
public bool DrawBorder { get; set; }

The appearance of the items displayed by the listbox control.

C#
[DataMember]
public ListBoxItemTheme ItemTheme { get; set; }

The area on the control where items are drawn.

C#
public Rectangle ItemsArea { get; set; }

Invokes the SelectedItemChanged event.

C#
protected virtual void OnSelectedItemChanged()

Invokes the SelectedItemExecuted event.

C#
protected virtual void OnItemAction()

Called when Resize(int, int) was called.

C#
protected override void OnResized()

Configures the associated ScrollBar.

C#
public void SetupScrollBar(Orientation orientation, int sizeValue, Point position)

orientation Orientation
The orientation of the scrollbar.

sizeValue int
The size of the scrollbar.

position SadRogue.Primitives.Point
The position of the scrollbar.

Scrolls the list to the item currently selected.

C#
public void ScrollToSelectedItem()

Called when the keyboard is used on this control.

C#
public override bool ProcessKeyboard(Keyboard info)

info Keyboard

bool

Called as the mouse moves around the control area. Raises the MouseMove event and calls the DetermineState() method.

C#
protected override void OnMouseIn(ControlBase.ControlMouseState state)

state ControlBase.ControlMouseState
The current mouse data

Called when the left mouse button is clicked. Raises the MouseButtonClicked event and calls the DetermineState() method.

C#
protected override void OnLeftMouseClicked(ControlBase.ControlMouseState state)

state ControlBase.ControlMouseState
The current mouse data

GetItemAndIndexUnderMouse(ControlMouseState)

Section titled “GetItemAndIndexUnderMouse(ControlMouseState)”

Returns the item under the mouse, and its array position.

C#
public (object? item, int itemIndex) GetItemAndIndexUnderMouse(ControlBase.ControlMouseState state)

state ControlBase.ControlMouseState
The mouse state.

(object item, int itemIndex)
A tuple containing the item and the item’s array position.

Exception
Thrown when the theme for the listbox isn’t based on ListBoxTheme.

Sets up the scroll bar for the listbox.

C#
protected virtual void SetupScrollBar()

Shows the scroll bar when there are too many items to display; otherwise, hides it.

C#
protected void ShowHideScrollBar()

Updates the ThemeState by calling RefreshTheme(Colors) with the provided colors. Override this method to adjust how colors are used by the ThemeState.

C#
protected override void RefreshThemeStateColors(Colors colors)

colors Colors
The colors to apply to the theme state.

Updates each control hosted by this control.

C#
public override void UpdateAndRedraw(TimeSpan time)

time TimeSpan
The game frame time delta.

An event that triggers when the SelectedItem property changes.

C#
public event EventHandler<ListBox.SelectedItemEventArgs>? SelectedItemChanged

EventHandler<ListBox.SelectedItemEventArgs>

An event that triggers when the SelectedItem property is reselected by the mouse.

C#
public event EventHandler<ListBox.SelectedItemEventArgs>? SelectedItemReselected

EventHandler<ListBox.SelectedItemEventArgs>

An event that triggers when an item is double clicked or the Enter key is pressed while the listbox has focus.

C#
public event EventHandler<ListBox.SelectedItemEventArgs>? SelectedItemExecuted

EventHandler<ListBox.SelectedItemEventArgs>