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
Container
Gets or sets the parent container that hosts this control.
public IContainer? Container { get; set; }
Property Value
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
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
Indent
Gets or sets the indent string for each level
public string Indent { get; set; }
Property Value
IsEnabled
Gets or sets whether the tree control is enabled and can be interacted with.
public bool IsEnabled { get; set; }
Property Value
Margin
Gets or sets the margin (spacing) around the control.
public Margin Margin { get; set; }
Property Value
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
RootNodes
Gets the collection of root nodes in the tree.
public ReadOnlyCollection<TreeNode> RootNodes { get; }
Property Value
SelectedIndex
Gets or sets the currently selected node index in the flattened nodes list.
public int SelectedIndex { get; set; }
Property Value
SelectedNode
Gets the currently selected node.
public TreeNode? SelectedNode { get; }
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
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
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
nodeTreeNodeThe node to add.
AddRootNode(string)
Adds a root node with the specified text.
public TreeNode AddRootNode(string text)
Parameters
textstringText 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
targetNodeTreeNodeThe 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
tagobjectThe 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
textstringThe text to search for.
searchRootTreeNodeOptional 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
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.
RemoveRootNode(TreeNode)
Removes a root node from the tree.
public bool RemoveRootNode(TreeNode node)
Parameters
nodeTreeNodeThe 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
nodeTreeNodeThe 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
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
NodeExpandCollapse
Event that fires when a tree node is expanded or collapsed.
public event EventHandler<TreeNodeEventArgs>? NodeExpandCollapse
Event Type
SelectedNodeChanged
Event that fires when the selected node changes.
public event EventHandler<TreeNodeEventArgs>? SelectedNodeChanged