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 : BaseControl, IDOMPaintable, INotifyPropertyChanged, IInteractiveControl, IFocusableControl, IMouseAwareControl, IWindowControl, IDisposable
- 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
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
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 override IContainer? Container { get; set; }
Property Value
ContentHeight
Gets the actual rendered height of the control based on cached content.
public int? ContentHeight { get; }
Property Value
- int?
The total number of lines including header, items, and margins, or null if not rendered.
ContentWidth
Gets the minimum width needed to display the control's content, including margins. Returns null if width cannot be determined. This is calculated based on content (text length, child controls, etc.) and represents the natural/intrinsic size.
public override int? ContentWidth { get; }
Property Value
- int?
FocusedBackgroundColor
Gets or sets the background color when the control has focus.
public Color FocusedBackgroundColor { get; set; }
Property Value
FocusedForegroundColor
Gets or sets the foreground color when the control has focus.
public Color FocusedForegroundColor { get; set; }
Property Value
ForegroundColor
Gets or sets the foreground color of the dropdown in its normal state.
public Color ForegroundColor { get; set; }
Property Value
HasFocus
public bool HasFocus { get; }
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
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
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
MaxVisibleItems
Gets or sets the maximum number of items visible in the dropdown list without scrolling.
public int MaxVisibleItems { 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
StringItems
Gets or sets the items as a list of strings for simplified access.
public List<string> StringItems { get; set; }
Property Value
WantsMouseEvents
Whether this control wants to receive mouse events
public bool WantsMouseEvents { get; }
Property Value
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()
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.
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 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.
Events
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
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<DropdownItem?>? SelectedItemChanged
Event Type
SelectedValueChanged
Occurs when the selected value (text) changes.
public event EventHandler<string?>? SelectedValueChanged