Class TabControl
- Namespace
- SharpConsoleUI.Controls
- Assembly
- SharpConsoleUI.dll
A tab control that displays multiple pages of content, with tab headers for switching between them. Uses visibility toggling to show/hide tab content efficiently.
public class TabControl : BaseControl, IDOMPaintable, INotifyPropertyChanged, IContainer, IMouseAwareControl, IInteractiveControl, IContainerControl, IFocusableControl, IWindowControl, IDisposable, IFocusableContainerWithHeader
- Inheritance
-
TabControl
- Implements
- Inherited Members
- Extension Methods
Constructors
TabControl()
Initializes a new instance of the TabControl class.
public TabControl()
Properties
ActiveTab
Gets the currently active tab page, or null if no tabs exist.
public TabPage? ActiveTab { get; }
Property Value
ActiveTabIndex
Gets or sets the index of the currently active tab.
public int ActiveTabIndex { get; set; }
Property Value
BackgroundColor
Gets or sets the background color for the container and its child controls.
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
Container
Gets or sets the parent container that hosts this control.
public override IContainer? Container { get; set; }
Property Value
ContentWidth
Gets the minimum width needed to display the control's content, including margins. Returns null if width cannot be determined. This is calculated based on content (text length, child controls, etc.) and represents the natural/intrinsic size.
public override int? ContentWidth { get; }
Property Value
- int?
ForegroundColor
Gets or sets the foreground (text) color for the container and its child controls.
public Color ForegroundColor { get; set; }
Property Value
GetConsoleWindowSystem
Gets the console window system instance, or null if not attached to a window system.
public ConsoleWindowSystem? GetConsoleWindowSystem { get; }
Property Value
HasFocus
public bool HasFocus { get; }
Property Value
HasTabs
Gets whether the control has any tabs.
public bool HasTabs { get; }
Property Value
HeaderStyle
Gets or sets the visual style used to render the tab header area.
public TabHeaderStyle HeaderStyle { get; set; }
Property Value
Height
Gets or sets the explicit height of the control. Minimum height is 2 (1 for header, 1 for content).
public override int? Height { get; set; }
Property Value
- int?
IsDirty
Gets or sets whether this container needs to be redrawn.
public bool IsDirty { get; set; }
Property Value
IsEnabled
Gets or sets whether this control is enabled and can receive input.
public bool IsEnabled { get; set; }
Property Value
SelectOnRightClick
Gets or sets whether a right-click on a tab header selects that tab before firing the MouseRightClick event. Default: false (preserves backward compatibility).
public bool SelectOnRightClick { get; set; }
Property Value
TabCount
Gets the number of tabs in the control.
public int TabCount { get; }
Property Value
TabHeaderHeight
Returns the number of rows consumed by the tab header (1 for Classic, 2 for Separator/AccentedSeparator).
public int TabHeaderHeight { get; }
Property Value
TabPages
Gets the read-only list of tab pages.
public IReadOnlyList<TabPage> TabPages { get; }
Property Value
TabTitles
Gets the titles of all tabs.
public IEnumerable<string> TabTitles { get; }
Property Value
WantsMouseEvents
Whether this control wants to receive mouse events
public bool WantsMouseEvents { get; }
Property Value
Methods
AddTab(string, IWindowControl, bool)
Adds a new tab to the control.
public void AddTab(string title, IWindowControl content, bool isClosable = false)
Parameters
titlestringThe title displayed in the tab header.
contentIWindowControlThe control to display when this tab is active.
isClosableboolWhen true, a × close button is shown in the tab header.
ClearTabs()
Removes all tabs from the control.
public void ClearTabs()
FindTab(string)
Finds a tab by title.
public TabPage? FindTab(string title)
Parameters
titlestringThe title to search for.
Returns
- TabPage
The tab page, or null if not found.
GetChildren()
Gets the direct child controls of this container. Does not recursively include grandchildren - recursion happens in caller.
public IReadOnlyList<IWindowControl> GetChildren()
Returns
- IReadOnlyList<IWindowControl>
Read-only list of direct child controls (may include other containers)
Remarks
IMPORTANT: For HorizontalGridControl, this should return columns AND splitters in order: [Column1, Splitter1, Column2, Splitter2, Column3]
Splitters are IInteractiveControl and should be included in Tab navigation.
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.
GetTab(int)
Gets a tab by index.
public TabPage? GetTab(int index)
Parameters
indexintThe index of the tab.
Returns
- TabPage
The tab page, or null if index is out of range.
GetVisibleHeightForControl(IWindowControl)
Gets the actual visible height for a control within the container viewport. Returns null if the control is not found or visibility cannot be determined.
public int? GetVisibleHeightForControl(IWindowControl control)
Parameters
controlIWindowControlThe control to check
Returns
- int?
The number of visible lines, or null if unknown
HasTab(string)
Checks if a tab with the specified title exists.
public bool HasTab(string title)
Parameters
titlestringThe title to search for.
Returns
- bool
True if a tab with the title exists, false otherwise.
InsertTab(int, string, IWindowControl)
Inserts a new tab at the specified index.
public void InsertTab(int index, string title, IWindowControl content)
Parameters
indexintThe index where the tab should be inserted.
titlestringThe title displayed in the tab header.
contentIWindowControlThe control to display when this tab is active.
Invalidate(bool, IWindowControl?)
Marks this container as needing to be redrawn.
public void Invalidate(bool redrawAll, IWindowControl? callerControl = null)
Parameters
redrawAllboolIf true, forces a complete redraw of all content.
callerControlIWindowControlThe control that triggered the invalidation, if any.
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.
NextTab()
Switches to the next tab (wraps around to first tab).
public void NextTab()
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
PreviousTab()
Switches to the previous tab (wraps around to last tab).
public void PreviousTab()
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
RemoveTab(int)
Removes a tab at the specified index.
public void RemoveTab(int index)
Parameters
indexintThe index of the tab to remove.
RemoveTab(string)
Removes the first tab with the specified title.
public bool RemoveTab(string title)
Parameters
titlestringThe title of the tab to remove.
Returns
- bool
True if a tab was removed, false otherwise.
RemoveTabAt(int)
Removes a tab at the specified index.
public void RemoveTabAt(int index)
Parameters
indexintThe index of the tab to remove.
SetTabContent(int, IWindowControl)
Sets the content of a tab at the specified index.
public void SetTabContent(int index, IWindowControl newContent)
Parameters
indexintThe index of the tab.
newContentIWindowControlThe new content control.
SetTabTitle(int, string)
Sets the title of a tab at the specified index.
public void SetTabTitle(int index, string newTitle)
Parameters
SwitchToTab(string)
Switches to a tab by title.
public bool SwitchToTab(string title)
Parameters
titlestringThe title of the tab to switch to.
Returns
- bool
True if the tab was found and switched to, false otherwise.
Events
MouseClick
Event fired when the control is clicked
public event EventHandler<MouseEventArgs>? MouseClick
Event Type
MouseDoubleClick
Event fired when the control is double-clicked
public event EventHandler<MouseEventArgs>? MouseDoubleClick
Event Type
MouseEnter
Event fired when the mouse enters the control area
public event EventHandler<MouseEventArgs>? MouseEnter
Event Type
MouseLeave
Event fired when the mouse leaves the control area
public event EventHandler<MouseEventArgs>? MouseLeave
Event Type
MouseMove
Event fired 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
TabAdded
Raised when a tab is added to the control.
public event EventHandler<TabEventArgs>? TabAdded
Event Type
TabChanged
Raised after the active tab has changed.
public event EventHandler<TabChangedEventArgs>? TabChanged
Event Type
TabChanging
Raised before the active tab changes. Can be canceled.
public event EventHandler<TabChangingEventArgs>? TabChanging
Event Type
TabCloseRequested
Raised when the user clicks the close (×) button on a closable tab. The tab is NOT automatically removed — subscribe and call RemoveTab to close it.
public event EventHandler<TabEventArgs>? TabCloseRequested
Event Type
TabRemoved
Raised when a tab is removed from the control.
public event EventHandler<TabEventArgs>? TabRemoved