Table of Contents

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

prompt string

The 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

prompt string

The prompt text displayed in the header.

items IEnumerable<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

prompt string

The prompt text displayed in the header.

items IEnumerable<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

bool

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

bool

CanReceiveFocus

Whether this control can receive focus

public bool CanReceiveFocus { get; }

Property Value

bool

Container

Gets or sets the parent container that hosts this control.

public IContainer? Container { get; set; }

Property Value

IContainer

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

bool

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

HorizontalAlignment

IsDropdownOpen

Gets or sets whether the dropdown list is currently expanded.

public bool IsDropdownOpen { get; set; }

Property Value

bool

IsEnabled

Gets or sets whether the dropdown is enabled and can be interacted with.

public bool IsEnabled { get; set; }

Property Value

bool

ItemFormatter

Gets or sets the custom item formatter delegate for rendering dropdown items.

public DropdownControl.ItemFormatterEvent? ItemFormatter { get; set; }

Property Value

DropdownControl.ItemFormatterEvent

Items

Gets or sets the list of dropdown items.

public List<DropdownItem> Items { get; set; }

Property Value

List<DropdownItem>

Margin

Gets or sets the margin around the control content.

public Margin Margin { get; set; }

Property Value

Margin

MaxVisibleItems

Gets or sets the maximum number of items visible in the dropdown list without scrolling.

public int MaxVisibleItems { get; set; }

Property Value

int

Name

Gets or sets the unique name identifier for this control, used for lookup.

public string? Name { get; set; }

Property Value

string

Prompt

Gets or sets the prompt text displayed in the dropdown header.

public string Prompt { get; set; }

Property Value

string

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

int

SelectedItem

Gets or sets the currently selected dropdown item. Returns null if no item is selected.

public DropdownItem? SelectedItem { get; set; }

Property Value

DropdownItem

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

string

StickyPosition

Gets or sets whether this control should stick to the top or bottom during scrolling.

public StickyPosition StickyPosition { get; set; }

Property Value

StickyPosition

StringItems

Gets or sets the items as a list of strings for simplified access.

public List<string> StringItems { get; set; }

Property Value

List<string>

Tag

Gets or sets an arbitrary object value that can be used to store custom data.

public object? Tag { get; set; }

Property Value

object

VerticalAlignment

Gets or sets the vertical alignment of the control within its container.

public VerticalAlignment VerticalAlignment { get; set; }

Property Value

VerticalAlignment

Visible

Gets or sets whether this control is visible.

public bool Visible { get; set; }

Property Value

bool

WantsMouseEvents

Whether this control wants to receive mouse events

public bool WantsMouseEvents { get; }

Property Value

bool

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

item DropdownItem

The dropdown item to add.

AddItem(string)

Adds a new item to the dropdown list with text only.

public void AddItem(string text)

Parameters

text string

The 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

text string

The text to display for the item.

icon string

The icon character or string to display.

iconColor Color?

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

constraints LayoutConstraints

The 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

buffer CharacterBuffer

The buffer to paint to.

bounds LayoutRect

The absolute bounds where the control should paint.

clipRect LayoutRect

The clipping rectangle (visible area).

defaultFg Color
defaultBg Color

ProcessKey(ConsoleKeyInfo)

Processes a keyboard input event.

public bool ProcessKey(ConsoleKeyInfo key)

Parameters

key ConsoleKeyInfo

The 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

args MouseEventArgs

Mouse 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

focus bool

Whether to give or remove focus

reason FocusReason

The reason for the focus change

Events

GotFocus

Event fired when the control gains focus

public event EventHandler? GotFocus

Event Type

EventHandler

LostFocus

Event fired when the control loses focus

public event EventHandler? LostFocus

Event Type

EventHandler

MouseClick

Event fired when the control is clicked

public event EventHandler<MouseEventArgs>? MouseClick

Event Type

EventHandler<MouseEventArgs>

MouseDoubleClick

Event fired when the control is double-clicked

public event EventHandler<MouseEventArgs>? MouseDoubleClick

Event Type

EventHandler<MouseEventArgs>

MouseEnter

Event fired when the mouse enters the control area

public event EventHandler<MouseEventArgs>? MouseEnter

Event Type

EventHandler<MouseEventArgs>

MouseLeave

Event fired when the mouse leaves the control area

public event EventHandler<MouseEventArgs>? MouseLeave

Event Type

EventHandler<MouseEventArgs>

MouseMove

Event fired when the mouse moves over the control

public event EventHandler<MouseEventArgs>? MouseMove

Event Type

EventHandler<MouseEventArgs>

SelectedIndexChanged

Occurs when the selected index changes.

public event EventHandler<int>? SelectedIndexChanged

Event Type

EventHandler<int>

SelectedItemChanged

Occurs when the selected item changes.

public event EventHandler<DropdownItem?>? SelectedItemChanged

Event Type

EventHandler<DropdownItem>

SelectedValueChanged

Occurs when the selected value (text) changes.

public event EventHandler<string?>? SelectedValueChanged

Event Type

EventHandler<string>