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 : IInteractiveControl, IFocusableControl, IWindowControl, IDisposable, IDOMPaintable
Inheritance
TreeControl
Implements
Inherited Members
Extension Methods

Constructors

TreeControl()

Initializes a new instance of the TreeControl class.

public TreeControl()

Properties

ActualWidth

Gets the actual rendered width in characters.

public int? ActualWidth { get; }

Property Value

int?

BackgroundColor

Gets or sets the background color of the tree control.

public Color BackgroundColor { get; set; }

Property Value

Color

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

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

Gets or sets whether this control currently has keyboard focus.

public bool HasFocus { get; set; }

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

HorizontalAlignment

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

public HorizontalAlignment HorizontalAlignment { get; set; }

Property Value

HorizontalAlignment

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

Margin

Gets or sets the margin (spacing) around the control.

public Margin Margin { get; set; }

Property Value

Margin

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?

Name

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

public string? Name { get; set; }

Property Value

string

RootNodes

Gets the collection of root nodes in the tree.

public ReadOnlyCollection<TreeNode> RootNodes { get; }

Property Value

ReadOnlyCollection<TreeNode>

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

StickyPosition

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

public StickyPosition StickyPosition { get; set; }

Property Value

StickyPosition

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

Width

Gets or sets the explicit width of the control, or null for automatic sizing.

public int? Width { get; set; }

Property Value

int?

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()

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

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 Size GetLogicalContentSize()

Returns

Size

The size representing the content's natural dimensions.

Invalidate()

Marks this control as needing to be re-rendered.

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.

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.

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

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>