Class DropdownControl
- Namespace
- SharpConsoleUI.Controls
- Assembly
- SharpConsoleUI.dll
A dropdown/combobox control that displays a list of selectable items. Supports keyboard navigation, type-ahead search, and custom item formatting.
public class DropdownControl : IInteractiveControl, IFocusableControl, IMouseAwareControl, IWindowControl, IDisposable, IDOMPaintable
- Inheritance
-
DropdownControl
- Implements
- Inherited Members
- Extension Methods
Constructors
DropdownControl(string)
Initializes a new instance of the DropdownControl class with only a prompt.
public DropdownControl(string prompt)
Parameters
promptstringThe prompt text displayed in the header.
DropdownControl(string, IEnumerable<DropdownItem>?)
Initializes a new instance of the DropdownControl class with dropdown items.
public DropdownControl(string prompt, IEnumerable<DropdownItem>? items = null)
Parameters
promptstringThe prompt text displayed in the header.
itemsIEnumerable<DropdownItem>Optional collection of DropdownItem objects to populate the dropdown.
DropdownControl(string, IEnumerable<string>?)
Initializes a new instance of the DropdownControl class with string items.
public DropdownControl(string prompt = "Select an item:", IEnumerable<string>? items = null)
Parameters
promptstringThe prompt text displayed in the header.
itemsIEnumerable<string>Optional collection of string items to populate the dropdown.
Properties
ActualHeight
Gets the actual rendered height of the control based on cached content.
public int? ActualHeight { get; }
Property Value
- int?
The total number of lines including header, items, and margins, or null if not rendered.
ActualWidth
Gets the actual rendered width of the control based on cached content.
public int? ActualWidth { get; }
Property Value
- int?
The maximum line width in characters, or null if content has not been rendered.
AutoAdjustWidth
Gets or sets whether the control automatically adjusts its width to fit content.
public bool AutoAdjustWidth { get; set; }
Property Value
BackgroundColor
Gets or sets the background color of the dropdown in its normal state.
public Color BackgroundColor { get; set; }
Property Value
- Color
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
Container
Gets or sets the parent container that hosts this control.
public IContainer? Container { get; set; }
Property Value
FocusedBackgroundColor
Gets or sets the background color when the control has focus.
public Color FocusedBackgroundColor { get; set; }
Property Value
- Color
FocusedForegroundColor
Gets or sets the foreground color when the control has focus.
public Color FocusedForegroundColor { get; set; }
Property Value
- Color
ForegroundColor
Gets or sets the foreground color of the dropdown in its normal state.
public Color ForegroundColor { get; set; }
Property Value
- Color
HasFocus
Gets or sets whether this control currently has keyboard focus.
public bool HasFocus { get; set; }
Property Value
HighlightBackgroundColor
Gets or sets the background color for the highlighted item in the dropdown list (keyboard or mouse).
public Color HighlightBackgroundColor { get; set; }
Property Value
- Color
HighlightForegroundColor
Gets or sets the foreground color for the highlighted item in the dropdown list (keyboard or mouse).
public Color HighlightForegroundColor { get; set; }
Property Value
- Color
HorizontalAlignment
Gets or sets the horizontal alignment of the control within its container.
public HorizontalAlignment HorizontalAlignment { get; set; }
Property Value
IsDropdownOpen
Gets or sets whether the dropdown list is currently expanded.
public bool IsDropdownOpen { get; set; }
Property Value
IsEnabled
Gets or sets whether the dropdown is enabled and can be interacted with.
public bool IsEnabled { get; set; }
Property Value
ItemFormatter
Gets or sets the custom item formatter delegate for rendering dropdown items.
public DropdownControl.ItemFormatterEvent? ItemFormatter { get; set; }
Property Value
Items
Gets or sets the list of dropdown items.
public List<DropdownItem> Items { get; set; }
Property Value
Margin
Gets or sets the margin around the control content.
public Margin Margin { get; set; }
Property Value
MaxVisibleItems
Gets or sets the maximum number of items visible in the dropdown list without scrolling.
public int MaxVisibleItems { get; set; }
Property Value
Name
Gets or sets the unique name identifier for this control, used for lookup.
public string? Name { get; set; }
Property Value
Prompt
Gets or sets the prompt text displayed in the dropdown header.
public string Prompt { get; set; }
Property Value
SelectedIndex
Gets or sets the index of the currently selected item. Returns -1 if no item is selected.
public int SelectedIndex { get; set; }
Property Value
SelectedItem
Gets or sets the currently selected dropdown item. Returns null if no item is selected.
public DropdownItem? SelectedItem { get; set; }
Property Value
SelectedValue
Gets or sets the text of the currently selected item. Returns null if no item is selected.
public string? SelectedValue { get; set; }
Property Value
StickyPosition
Gets or sets whether this control should stick to the top or bottom during scrolling.
public StickyPosition StickyPosition { get; set; }
Property Value
StringItems
Gets or sets the items as a list of strings for simplified access.
public List<string> StringItems { get; set; }
Property Value
Tag
Gets or sets an arbitrary object value that can be used to store custom data.
public object? Tag { get; set; }
Property Value
VerticalAlignment
Gets or sets the vertical alignment of the control within its container.
public VerticalAlignment VerticalAlignment { get; set; }
Property Value
Visible
Gets or sets whether this control is visible.
public bool Visible { get; set; }
Property Value
WantsMouseEvents
Whether this control wants to receive mouse events
public bool WantsMouseEvents { get; }
Property Value
Width
Gets or sets the fixed width of the control. When null, the control auto-sizes based on content.
public int? Width { get; set; }
Property Value
- int?
Methods
AddItem(DropdownItem)
Adds a new item to the dropdown list.
public void AddItem(DropdownItem item)
Parameters
itemDropdownItemThe dropdown item to add.
AddItem(string)
Adds a new item to the dropdown list with text only.
public void AddItem(string text)
Parameters
textstringThe text to display for the item.
AddItem(string, string, Color?)
Adds a new item to the dropdown list with text and an optional icon.
public void AddItem(string text, string icon, Color? iconColor = null)
Parameters
textstringThe text to display for the item.
iconstringThe icon character or string to display.
iconColorColor?Optional color for the icon.
ClearItems()
Removes all items from the dropdown list and resets selection state.
public void ClearItems()
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
GetLogicalContentSize()
Gets the logical size of the control's content without rendering.
public Size GetLogicalContentSize()
Returns
- Size
The size representing the content's natural dimensions.
Invalidate()
Invalidates the cached content, forcing a re-render on the next draw.
public void Invalidate()
MeasureDOM(LayoutConstraints)
Measures the control's desired size given the available constraints.
public LayoutSize MeasureDOM(LayoutConstraints constraints)
Parameters
constraintsLayoutConstraintsThe layout constraints (min/max width/height).
Returns
- LayoutSize
The desired size of the control.
PaintDOM(CharacterBuffer, LayoutRect, LayoutRect, Color, Color)
Paints the control's content directly to a CharacterBuffer.
public 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 mouse events for the dropdown header. Note: When the dropdown list is open, it's rendered as a portal and receives events via ProcessPortalMouseEvent instead.
public bool ProcessMouseEvent(MouseEventArgs args)
Parameters
argsMouseEventArgsMouse event arguments with control-relative coordinates.
Returns
- bool
True if the event was handled.
SetFocus(bool, FocusReason)
Sets focus to this control
public void SetFocus(bool focus, FocusReason reason = FocusReason.Programmatic)
Parameters
focusboolWhether to give or remove focus
reasonFocusReasonThe reason for the focus change
Events
GotFocus
Event fired when the control gains focus
public event EventHandler? GotFocus
Event Type
LostFocus
Event fired when the control loses focus
public event EventHandler? LostFocus
Event Type
MouseClick
Event fired when the control is clicked
public event EventHandler<MouseEventArgs>? MouseClick
Event Type
MouseDoubleClick
Event fired when the control is double-clicked
public event EventHandler<MouseEventArgs>? MouseDoubleClick
Event Type
MouseEnter
Event fired when the mouse enters the control area
public event EventHandler<MouseEventArgs>? MouseEnter
Event Type
MouseLeave
Event fired when the mouse leaves the control area
public event EventHandler<MouseEventArgs>? MouseLeave
Event Type
MouseMove
Event fired when the mouse moves over the control
public event EventHandler<MouseEventArgs>? MouseMove
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<DropdownItem?>? SelectedItemChanged
Event Type
SelectedValueChanged
Occurs when the selected value (text) changes.
public event EventHandler<string?>? SelectedValueChanged