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
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
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
Guide
Gets or sets the tree guide style used for drawing the tree structure.
public TreeGuide Guide { get; set; }
Property Value
HasFocus
public bool HasFocus { get; }
Property Value
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
HighlightForegroundColor
Gets or sets the foreground color for highlighted items
public Color HighlightForegroundColor { 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
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
ScrollbarVisibility
public ScrollbarVisibility ScrollbarVisibility { get; set; }
Property Value
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
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
WantsMouseEvents
Whether this control wants to receive mouse events
public bool WantsMouseEvents { get; }
Property Value
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()
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 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.
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
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 a mouse event for this control
public bool ProcessMouseEvent(MouseEventArgs args)
Parameters
argsMouseEventArgsMouse 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
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.
Events
MouseClick
Occurs when the mouse is clicked on the control.
public event EventHandler<MouseEventArgs>? MouseClick
Event Type
MouseDoubleClick
Occurs when the mouse is double-clicked on the control.
public event EventHandler<MouseEventArgs>? MouseDoubleClick
Event Type
MouseEnter
Occurs when the mouse enters the control area.
public event EventHandler<MouseEventArgs>? MouseEnter
Event Type
MouseLeave
Occurs when the mouse leaves the control area.
public event EventHandler<MouseEventArgs>? MouseLeave
Event Type
MouseMove
Occurs 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
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
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