Table of Contents

Class TreeControl

Namespace
SharpConsoleUI.Controls
Assembly
SharpConsoleUI.dll

A hierarchical tree control that displays nodes in a collapsible tree structure with keyboard navigation.

public class TreeControl : BaseControl, IDOMPaintable, INotifyPropertyChanged, IInteractiveControl, IFocusableControl, IMouseAwareControl, IWindowControl, IDisposable
Inheritance
TreeControl
Implements
Inherited Members
Extension Methods

Constructors

TreeControl()

Initializes a new instance of the TreeControl class.

public TreeControl()

Properties

BackgroundColor

Gets or sets the background color of the tree control.

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

ContentWidth

Gets the actual rendered width in characters.

public override int? ContentWidth { get; }

Property Value

int?

ForegroundColor

Gets or sets the foreground color of the tree control.

public Color ForegroundColor { get; set; }

Property Value

Color

Guide

Gets or sets the tree guide style used for drawing the tree structure.

public TreeGuide Guide { get; set; }

Property Value

TreeGuide

HasFocus

public bool HasFocus { get; }

Property Value

bool

Height

Gets or sets the explicit height of the control. If null, control height is based on content until available height.

public override int? Height { get; set; }

Property Value

int?

HighlightBackgroundColor

Gets or sets the background color for highlighted items

public Color? HighlightBackgroundColor { get; set; }

Property Value

Color?

HighlightForegroundColor

Gets or sets the foreground color for highlighted items

public Color HighlightForegroundColor { get; set; }

Property Value

Color

Indent

Gets or sets the indent string for each level

public string Indent { get; set; }

Property Value

string

IsEnabled

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

public bool IsEnabled { get; set; }

Property Value

bool

MaxVisibleItems

Gets or sets the maximum number of items to display at once. If null, shows as many as will fit in available height.

public int? MaxVisibleItems { get; set; }

Property Value

int?

RootNodes

Gets the collection of root nodes in the tree.

public ReadOnlyCollection<TreeNode> RootNodes { get; }

Property Value

ReadOnlyCollection<TreeNode>

ScrollbarVisibility

public ScrollbarVisibility ScrollbarVisibility { get; set; }

Property Value

ScrollbarVisibility

SelectOnRightClick

Gets or sets whether a right-click selects the node under the mouse cursor before firing the MouseRightClick event. Default: false (preserves backward compatibility).

public bool SelectOnRightClick { get; set; }

Property Value

bool

SelectedIndex

Gets or sets the currently selected node index in the flattened nodes list.

public int SelectedIndex { get; set; }

Property Value

int

SelectedNode

Gets the currently selected node.

public TreeNode? SelectedNode { get; }

Property Value

TreeNode

WantsMouseEvents

Whether this control wants to receive mouse events

public bool WantsMouseEvents { get; }

Property Value

bool

Methods

AddRootNode(TreeNode)

Adds a root node to the tree control.

public void AddRootNode(TreeNode node)

Parameters

node TreeNode

The node to add.

AddRootNode(string)

Adds a root node with the specified text.

public TreeNode AddRootNode(string text)

Parameters

text string

Text for the new node.

Returns

TreeNode

The newly created node.

Clear()

Clears all nodes from the tree.

public void Clear()

CollapseAll()

Collapses all nodes in the tree.

public void CollapseAll()

EnsureNodeVisible(TreeNode)

Ensures a node is visible by expanding all its parent nodes.

public bool EnsureNodeVisible(TreeNode targetNode)

Parameters

targetNode TreeNode

The node to make visible.

Returns

bool

True if the node was found and made visible, false otherwise.

ExpandAll()

Expands all nodes in the tree.

public void ExpandAll()

FindNodeByTag(object)

Finds a node by its associated tag object.

public TreeNode? FindNodeByTag(object tag)

Parameters

tag object

The tag object to search for.

Returns

TreeNode

The first node found with matching tag, or null if not found.

FindNodeByText(string, TreeNode?)

Finds a node by its text content (exact match).

public TreeNode? FindNodeByText(string text, TreeNode? searchRoot = null)

Parameters

text string

The text to search for.

searchRoot TreeNode

Optional root to search from; searches all nodes if null.

Returns

TreeNode

The first node found with matching text, or null if not found.

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

constraints LayoutConstraints

The 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

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 a mouse event for this control

public bool ProcessMouseEvent(MouseEventArgs args)

Parameters

args MouseEventArgs

Mouse event arguments with control-relative coordinates

Returns

bool

True if the event was handled and should not propagate further

RemoveRootNode(TreeNode)

Removes a root node from the tree.

public bool RemoveRootNode(TreeNode node)

Parameters

node TreeNode

The node to remove.

Returns

bool

True if the node was found and removed, false otherwise.

SelectNode(TreeNode)

Selects a specific node in the tree.

public bool SelectNode(TreeNode node)

Parameters

node TreeNode

The node to select.

Returns

bool

True if the node was found and selected, false otherwise.

Events

MouseClick

Occurs when the mouse is clicked on the control.

public event EventHandler<MouseEventArgs>? MouseClick

Event Type

EventHandler<MouseEventArgs>

MouseDoubleClick

Occurs when the mouse is double-clicked on the control.

public event EventHandler<MouseEventArgs>? MouseDoubleClick

Event Type

EventHandler<MouseEventArgs>

MouseEnter

Occurs when the mouse enters the control area.

public event EventHandler<MouseEventArgs>? MouseEnter

Event Type

EventHandler<MouseEventArgs>

MouseLeave

Occurs when the mouse leaves the control area.

public event EventHandler<MouseEventArgs>? MouseLeave

Event Type

EventHandler<MouseEventArgs>

MouseMove

Occurs when the mouse moves over the control.

public event EventHandler<MouseEventArgs>? MouseMove

Event Type

EventHandler<MouseEventArgs>

MouseRightClick

Occurs when the control is right-clicked with the mouse.

public event EventHandler<MouseEventArgs>? MouseRightClick

Event Type

EventHandler<MouseEventArgs>

NodeActivated

Event that fires when a node is activated (double-clicked or Enter pressed on a leaf node).

public event EventHandler<TreeNodeEventArgs>? NodeActivated

Event Type

EventHandler<TreeNodeEventArgs>

NodeExpandCollapse

Event that fires when a tree node is expanded or collapsed.

public event EventHandler<TreeNodeEventArgs>? NodeExpandCollapse

Event Type

EventHandler<TreeNodeEventArgs>

SelectedNodeChanged

Event that fires when the selected node changes.

public event EventHandler<TreeNodeEventArgs>? SelectedNodeChanged

Event Type

EventHandler<TreeNodeEventArgs>