Class ListControl
- Namespace
- SharpConsoleUI.Controls
- Assembly
- SharpConsoleUI.dll
A scrollable list control that supports selection, highlighting, and keyboard navigation.
public class ListControl : BaseControl, IDOMPaintable, INotifyPropertyChanged, IInteractiveControl, IFocusableControl, IMouseAwareControl, IWindowControl, IDisposable
- Inheritance
-
ListControl
- Implements
- Inherited Members
- Extension Methods
Constructors
ListControl()
Initializes a new empty ListControl with no title.
public ListControl()
ListControl(IEnumerable<ListItem>?)
Initializes a new ListControl with ListItem objects and no title.
public ListControl(IEnumerable<ListItem>? items)
Parameters
itemsIEnumerable<ListItem>The initial ListItem objects to populate the list.
ListControl(IEnumerable<string>?)
Initializes a new ListControl with string items and no title.
public ListControl(IEnumerable<string>? items)
Parameters
itemsIEnumerable<string>The initial items to populate the list.
ListControl(string)
Initializes a new empty ListControl with a title.
public ListControl(string title)
Parameters
titlestringThe title displayed at the top of the list.
ListControl(string?, IEnumerable<ListItem>?)
Initializes a new ListControl with a title and ListItem objects.
public ListControl(string? title, IEnumerable<ListItem>? items)
Parameters
titlestringThe title displayed at the top of the list.
itemsIEnumerable<ListItem>The initial ListItem objects to populate the list.
ListControl(string?, IEnumerable<string>?)
Initializes a new ListControl with a title and string items.
public ListControl(string? title, IEnumerable<string>? items)
Parameters
titlestringThe title displayed at the top of the list.
itemsIEnumerable<string>The initial items to populate the list.
Properties
AutoAdjustWidth
Gets or sets whether the control automatically adjusts its width to fit content.
public bool AutoAdjustWidth { get; set; }
Property Value
AutoHighlightOnFocus
Gets or sets whether to auto-highlight on focus gain. When true, the control will highlight the selected item (or first item) when focused. Default: true (fixes UX issue where focus had no visual feedback).
public bool AutoHighlightOnFocus { get; set; }
Property Value
BackgroundColor
Gets or sets the background color of the list control.
public Color? BackgroundColor { get; set; }
Property Value
CanFocusWithMouse
Whether this control can receive focus via mouse clicks
public bool CanFocusWithMouse { get; }
Property Value
CanReceiveFocus
Whether this control can receive focus
public bool CanReceiveFocus { get; }
Property Value
CheckboxMode
Gets or sets whether items display checkboxes ([x]/[ ]) and Space toggles checked state.
public bool CheckboxMode { get; set; }
Property Value
ContentHeight
Gets the actual rendered height in lines.
public int? ContentHeight { get; }
Property Value
- int?
ContentWidth
Gets the actual rendered width in characters.
public override int? ContentWidth { get; }
Property Value
- int?
DoubleClickActivates
Gets or sets whether double-click activates items. Default: true.
public bool DoubleClickActivates { get; set; }
Property Value
DoubleClickThresholdMs
Gets or sets the double-click threshold in milliseconds. Default: 500.
public int DoubleClickThresholdMs { get; set; }
Property Value
FocusedBackgroundColor
Gets or sets the background color when the list has focus.
public Color? FocusedBackgroundColor { get; set; }
Property Value
FocusedForegroundColor
Gets or sets the foreground color when the list has focus.
public Color FocusedForegroundColor { get; set; }
Property Value
ForegroundColor
Gets or sets the foreground color of the list control.
public Color ForegroundColor { get; set; }
Property Value
HasFocus
public bool HasFocus { get; }
Property Value
HighlightBackgroundColor
Gets or sets the background color for highlighted items.
public Color HighlightBackgroundColor { get; set; }
Property Value
HighlightForegroundColor
Gets or sets the foreground color for highlighted items.
public Color HighlightForegroundColor { get; set; }
Property Value
HoverHighlightsItems
Gets or sets whether mouse hover highlights items visually. Default: true.
public bool HoverHighlightsItems { get; set; }
Property Value
HoveredIndex
Gets the index of the currently hovered item (mouse cursor). -1 if no item is hovered.
public int HoveredIndex { get; }
Property Value
IsEnabled
Gets or sets whether the list control is enabled and can be interacted with.
public bool IsEnabled { get; set; }
Property Value
IsSelectable
Gets or sets whether items can be selected in the list.
public bool IsSelectable { get; set; }
Property Value
ItemFormatter
Gets or sets a custom formatter for rendering list items.
public ListControl.ItemFormatterEvent? ItemFormatter { get; set; }
Property Value
Items
Gets or sets the collection of items displayed in the list.
public List<ListItem> Items { get; set; }
Property Value
MaxVisibleItems
Gets or sets the maximum number of items visible at once. If null, calculated from available height.
public int? MaxVisibleItems { get; set; }
Property Value
- int?
MouseWheelScrollSpeed
Gets or sets the number of lines to scroll with mouse wheel. Default: 3.
public int MouseWheelScrollSpeed { get; set; }
Property Value
ScrollbarVisibility
Gets or sets when the vertical scrollbar should be displayed. Default: Auto (shows only when content exceeds viewport).
public ScrollbarVisibility ScrollbarVisibility { get; set; }
Property Value
SelectOnRightClick
Gets or sets whether a right-click selects the item under the mouse cursor before firing the MouseRightClick event. Default: false (preserves backward compatibility).
public bool SelectOnRightClick { get; set; }
Property Value
SelectedIndex
Gets or sets the index of the currently selected item. -1 if no item is selected.
public int SelectedIndex { get; set; }
Property Value
SelectedItem
Gets or sets the currently selected item.
public ListItem? SelectedItem { get; set; }
Property Value
SelectedValue
Gets or sets the text of the currently selected item.
public string? SelectedValue { get; set; }
Property Value
StringItems
Gets or sets the list items as simple strings.
public List<string> StringItems { get; set; }
Property Value
Title
Gets or sets the title displayed at the top of the list.
public string Title { get; set; }
Property Value
WantsMouseEvents
Whether this control wants to receive mouse events
public bool WantsMouseEvents { get; }
Property Value
Methods
AddItem(ListItem)
Adds a ListItem to the list.
public void AddItem(ListItem item)
Parameters
itemListItemThe item to add.
AddItem(string)
Adds a new item with the specified text.
public void AddItem(string text)
Parameters
textstringThe text of the item.
AddItem(string, string?, Color?)
Adds a new item with text, optional icon, and icon color.
public void AddItem(string text, string? icon = null, Color? iconColor = null)
Parameters
textstringThe text of the item.
iconstringOptional icon to display.
iconColorColor?Optional color for the icon.
ClearItems()
Removes all items from the list.
public void ClearItems()
Create()
Creates a fluent builder for constructing a ListControl.
public static ListBuilder Create()
Returns
- ListBuilder
A new ListBuilder instance.
GetCheckedItems()
Returns all items where IsChecked is true.
public List<ListItem> GetCheckedItems()
Returns
GetLogicalContentSize()
Gets the logical size of the control's content without rendering.
public override Size GetLogicalContentSize()
Returns
- Size
The size representing the content's natural dimensions.
MeasureDOM(LayoutConstraints)
Measures the control's desired size given the available constraints.
public override LayoutSize MeasureDOM(LayoutConstraints constraints)
Parameters
constraintsLayoutConstraintsThe layout constraints (min/max width/height).
Returns
- LayoutSize
The desired size of the control.
OnDisposing()
Called during Dispose() before Container is set to null.
Override to perform control-specific cleanup (null events, close portals, clear data, etc.).
protected override void OnDisposing()
PaintDOM(CharacterBuffer, LayoutRect, LayoutRect, Color, Color)
Paints the control's content directly to a CharacterBuffer.
public override void PaintDOM(CharacterBuffer buffer, LayoutRect bounds, LayoutRect clipRect, Color defaultFg, Color defaultBg)
Parameters
bufferCharacterBufferThe buffer to paint to.
boundsLayoutRectThe absolute bounds where the control should paint.
clipRectLayoutRectThe clipping rectangle (visible area).
defaultFgColordefaultBgColor
ProcessKey(ConsoleKeyInfo)
Processes a keyboard input event.
public bool ProcessKey(ConsoleKeyInfo key)
Parameters
keyConsoleKeyInfoThe key information for the pressed key.
Returns
- bool
True if the key was handled by this control; otherwise, false.
ProcessMouseEvent(MouseEventArgs)
Processes a mouse event for this control
public bool ProcessMouseEvent(MouseEventArgs args)
Parameters
argsMouseEventArgsMouse event arguments with control-relative coordinates
Returns
- bool
True if the event was handled and should not propagate further
SetAllChecked(bool)
Sets all items' IsChecked state and fires CheckedItemsChanged once.
public void SetAllChecked(bool value)
Parameters
valuebool
Events
CheckedItemsChanged
Occurs when the checked state of any item changes.
public event EventHandler<EventArgs>? CheckedItemsChanged
Event Type
ItemActivated
Occurs when an item is activated (Enter or double-click).
public event EventHandler<ListItem>? ItemActivated
Event Type
ItemHovered
Occurs when an item is hovered by the mouse. The index is the hovered item index, or -1 if mouse left all items.
public event EventHandler<int>? ItemHovered
Event Type
MouseClick
Occurs when the control is clicked with the mouse.
public event EventHandler<MouseEventArgs>? MouseClick
Event Type
MouseDoubleClick
Occurs when an item is double-clicked with the mouse.
public event EventHandler<MouseEventArgs>? MouseDoubleClick
Event Type
MouseEnter
Occurs when the mouse enters the control area.
public event EventHandler<MouseEventArgs>? MouseEnter
Event Type
MouseLeave
Occurs when the mouse leaves the control area.
public event EventHandler<MouseEventArgs>? MouseLeave
Event Type
MouseMove
Occurs when the mouse moves over the control.
public event EventHandler<MouseEventArgs>? MouseMove
Event Type
MouseRightClick
Occurs when the control is right-clicked with the mouse.
public event EventHandler<MouseEventArgs>? MouseRightClick
Event Type
SelectedIndexChanged
Occurs when the selected index changes.
public event EventHandler<int>? SelectedIndexChanged
Event Type
SelectedItemChanged
Occurs when the selected item changes.
public event EventHandler<ListItem?>? SelectedItemChanged
Event Type
SelectedValueChanged
Occurs when the selected value (text) changes.
public event EventHandler<string?>? SelectedValueChanged