Table of Contents

Class MultilineEditControl

Namespace
SharpConsoleUI.Controls
Assembly
SharpConsoleUI.dll

A multiline text editing control with support for text selection, scrolling, and word wrap. Provides full cursor navigation, cut/copy/paste-like operations, and configurable scrollbars.

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

Constructors

MultilineEditControl(int)

Initializes a new instance of the MultilineEditControl with the specified viewport height.

public MultilineEditControl(int viewportHeight = 10)

Parameters

viewportHeight int

The number of visible lines in the viewport.

MultilineEditControl(string, int)

Initializes a new instance of the MultilineEditControl with initial content and viewport height.

public MultilineEditControl(string initialContent, int viewportHeight = 10)

Parameters

initialContent string

The initial text content to display.

viewportHeight int

The number of visible lines in the viewport.

Properties

AutoIndent

Gets or sets whether auto-indent is enabled. When enabled, pressing Enter copies leading whitespace from the current line to the new line.

public bool AutoIndent { get; set; }

Property Value

bool

BackgroundColor

Gets or sets the background color when the control is not focused.

public Color? BackgroundColor { get; set; }

Property Value

Color?

BorderColor

Gets or sets the border color of the control.

public Color BorderColor { 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

Content

Gets or sets the text content as a single string with line breaks.

public string Content { get; set; }

Property Value

string

ContentWidth

Gets the actual rendered width of the control content in characters.

public override int? ContentWidth { get; }

Property Value

int?

CurrentColumn

Gets the current cursor column number (1-based).

public int CurrentColumn { get; }

Property Value

int

CurrentLine

Gets the current cursor line number (1-based).

public int CurrentLine { get; }

Property Value

int

CurrentLineHighlightColor

Gets or sets the background color used to highlight the current line. Defaults to a slightly lighter shade of the editor background.

public Color CurrentLineHighlightColor { get; set; }

Property Value

Color

CurrentMatchIndex

Gets the 0-based index of the current match, or -1 if no current match.

public int CurrentMatchIndex { get; }

Property Value

int

EscapeExitsEditMode

When true, pressing Escape will not exit editing mode. Useful for IDE-style editors where Escape is used for other purposes (e.g. dismissing popups).

public bool EscapeExitsEditMode { get; set; }

Property Value

bool

FocusedBackgroundColor

Gets or sets the background color when the control is focused.

public Color? FocusedBackgroundColor { get; set; }

Property Value

Color?

FocusedForegroundColor

Gets or sets the foreground color when the control is focused.

public Color FocusedForegroundColor { get; set; }

Property Value

Color

ForegroundColor

Gets or sets the foreground color when the control is not focused.

public Color ForegroundColor { get; set; }

Property Value

Color

GutterRenderers

Gets the list of gutter renderers, rendered left-to-right.

public IReadOnlyList<IGutterRenderer> GutterRenderers { get; }

Property Value

IReadOnlyList<IGutterRenderer>

GutterWidth

Gets the width of the line-number gutter in columns (0 when ShowLineNumbers is false).

public int GutterWidth { get; }

Property Value

int

HasActiveSearch

Gets whether search highlighting is currently active.

public bool HasActiveSearch { get; }

Property Value

bool

HasFocus

public bool HasFocus { get; }

Property Value

bool

HighlightCurrentLine

Gets or sets whether the current line is visually highlighted.

public bool HighlightCurrentLine { get; set; }

Property Value

bool

HorizontalScrollOffset

Gets the current horizontal scroll offset (columns scrolled from left of document).

public int HorizontalScrollOffset { get; }

Property Value

int

HorizontalScrollbarVisibility

Gets or sets when the horizontal scrollbar is displayed.

public ScrollbarVisibility HorizontalScrollbarVisibility { get; set; }

Property Value

ScrollbarVisibility

IsEditing

Gets or sets whether the control is currently in text editing mode.

public bool IsEditing { get; set; }

Property Value

bool

IsEnabled

Gets or sets whether this control is enabled and can receive input.

public bool IsEnabled { get; set; }

Property Value

bool

IsModified

Gets whether the content has been modified since the last save point.

public bool IsModified { get; }

Property Value

bool

LineCount

Gets the total number of lines in the content.

public int LineCount { get; }

Property Value

int

LineHighlights

Gets or sets per-line background highlights keyed by 0-based source line index.

public Dictionary<int, Color> LineHighlights { get; set; }

Property Value

Dictionary<int, Color>

LineNumberColor

Gets or sets the foreground color for line numbers in the gutter.

public Color LineNumberColor { get; set; }

Property Value

Color

MatchCount

Gets the number of search matches found.

public int MatchCount { get; }

Property Value

int

MaxLength

Gets or sets the maximum total character length of the content. Null means no limit. Enforced on all insertion operations.

public int? MaxLength { get; set; }

Property Value

int?

OverwriteMode

Gets or sets whether overwrite mode is active. In overwrite mode, typed characters replace the character at the cursor instead of inserting. Toggle with the Insert key.

public bool OverwriteMode { get; set; }

Property Value

bool

PlaceholderText

Gets or sets the placeholder text shown when the control is empty and not editing.

public string? PlaceholderText { get; set; }

Property Value

string

PreferredCursorShape

Gets the preferred cursor shape based on editing state. Returns VerticalBar when editing (like modern text editors), null otherwise.

public CursorShape? PreferredCursorShape { get; }

Property Value

CursorShape?

ReadOnly

Gets or sets whether the control is in read-only mode. When read-only, users can navigate and select text, but cannot modify content.

public bool ReadOnly { get; set; }

Property Value

bool

ScrollbarColor

Gets or sets the scrollbar track color.

public Color ScrollbarColor { get; set; }

Property Value

Color

ScrollbarThumbColor

Gets or sets the scrollbar thumb (handle) color.

public Color ScrollbarThumbColor { get; set; }

Property Value

Color

SearchTerm

Gets the current search term, or null if no search is active.

public string? SearchTerm { get; }

Property Value

string

SelectionBackgroundColor

Gets or sets the background color for selected text.

public Color SelectionBackgroundColor { get; set; }

Property Value

Color

SelectionForegroundColor

Gets or sets the foreground color for selected text.

public Color SelectionForegroundColor { get; set; }

Property Value

Color

ShowEditingHints

Gets or sets whether editing mode hints are shown at the bottom-right of the viewport. When enabled, shows "Enter to edit" in browse mode and "Esc to stop editing" in editing mode.

public bool ShowEditingHints { get; set; }

Property Value

bool

ShowLineNumbers

Gets or sets whether line numbers are displayed in a gutter on the left side. When enabled, a built-in LineNumberGutterRenderer is inserted at index 0.

public bool ShowLineNumbers { get; set; }

Property Value

bool

ShowWhitespace

Gets or sets whether whitespace characters are rendered with visible markers. Spaces are shown as middle dots.

public bool ShowWhitespace { get; set; }

Property Value

bool

SyntaxHighlighter

Gets or sets the syntax highlighter used to colorize content. Set to null to disable syntax highlighting.

public ISyntaxHighlighter? SyntaxHighlighter { get; set; }

Property Value

ISyntaxHighlighter

TabSize

Gets or sets the number of spaces used for tab indentation.

public int TabSize { get; set; }

Property Value

int

UndoLimit

Gets or sets the maximum number of undo actions retained.

public int UndoLimit { get; set; }

Property Value

int

VerticalScrollOffset

Gets the current vertical scroll offset (lines scrolled from top of document).

public int VerticalScrollOffset { get; }

Property Value

int

VerticalScrollbarVisibility

Gets or sets when the vertical scrollbar is displayed.

public ScrollbarVisibility VerticalScrollbarVisibility { get; set; }

Property Value

ScrollbarVisibility

ViewportHeight

Gets or sets the number of visible lines in the viewport.

public int ViewportHeight { get; set; }

Property Value

int

WantsMouseEvents

Whether this control wants to receive mouse events

public bool WantsMouseEvents { get; }

Property Value

bool

WantsTabKey

When in edit mode and not read-only, Tab/Shift+Tab are handled by the editor (indent/dedent) instead of being intercepted for focus traversal.

public bool WantsTabKey { get; }

Property Value

bool

Width

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

public override int? Width { get; set; }

Property Value

int?

WrapMode

Gets or sets the text wrapping mode.

public WrapMode WrapMode { get; set; }

Property Value

WrapMode

Methods

AddGutterRenderer(IGutterRenderer)

Adds a gutter renderer to the end of the renderer list.

public void AddGutterRenderer(IGutterRenderer renderer)

Parameters

renderer IGutterRenderer

AppendContent(string)

Appends content to the end of the control and scrolls to make it visible.

public void AppendContent(string content)

Parameters

content string

The content to append.

AppendContentLines(List<string>)

Appends multiple lines to the end of the control and scrolls to make them visible.

public void AppendContentLines(List<string> lines)

Parameters

lines List<string>

The lines to append.

ClearFind()

Clears the current search, removing all match highlighting.

public void ClearFind()

ClearGutterRenderers()

Removes all gutter renderers. Also clears the built-in line number renderer if present.

public void ClearGutterRenderers()

ClearLineHighlights()

Clears all per-line background highlights.

public void ClearLineHighlights()

ClearSelection()

Clears the current text selection.

public void ClearSelection()

DeleteCharsBefore(int)

Deletes up to count characters immediately before the cursor on the current line. Stops at column 0 (does not merge lines).

public void DeleteCharsBefore(int count)

Parameters

count int

EnsureCursorVisible()

Ensures the cursor position is visible within the viewport by adjusting scroll offsets.

public void EnsureCursorVisible()

Find(string, bool, bool)

Searches the content for all occurrences of the given term. Highlights all matches and navigates to the first match at or after the cursor position.

public int Find(string term, bool caseSensitive = false, bool useRegex = false)

Parameters

term string

The text to search for.

caseSensitive bool

Whether the search is case-sensitive.

useRegex bool

Whether to treat the term as a regular expression.

Returns

int

The number of matches found.

FindNext()

Navigates to the next search match. Wraps around to the beginning.

public bool FindNext()

Returns

bool

True if a next match was found, false if no search is active.

FindPrevious()

Navigates to the previous search match. Wraps around to the end.

public bool FindPrevious()

Returns

bool

True if a previous match was found, false if no search is active.

GetContent()

Gets the text content as a single string with line breaks.

public string GetContent()

Returns

string

The complete text content.

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.

GetLogicalCursorPosition()

Gets the logical cursor position within the control's content coordinate system. This should be the raw position without any visual adjustments for margins, scrolling, etc.

public Point? GetLogicalCursorPosition()

Returns

Point?

Logical cursor position or null if no cursor.

GetSelectedText()

Gets the currently selected text.

public string GetSelectedText()

Returns

string

The selected text, or an empty string if no selection exists.

GoToEnd()

Moves the cursor to the end of the document content and ensures it's visible.

public void GoToEnd()

GoToLine(int)

Moves the cursor to the specified line number (1-based). Clamps to valid range. Clears any active selection.

public void GoToLine(int lineNumber)

Parameters

lineNumber int

The 1-based line number to navigate to.

InsertGutterRenderer(int, IGutterRenderer)

Inserts a gutter renderer at the specified index.

public void InsertGutterRenderer(int index, IGutterRenderer renderer)

Parameters

index int
renderer IGutterRenderer

InsertText(string)

Insert text at the current cursor position

public void InsertText(string text)

Parameters

text string

MarkAsSaved()

Marks the current content as the saved state for IsModified tracking.

public void MarkAsSaved()

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

RemoveGutterRenderer(IGutterRenderer)

Removes a gutter renderer from the list.

public bool RemoveGutterRenderer(IGutterRenderer renderer)

Parameters

renderer IGutterRenderer

Returns

bool

Replace(string)

Replaces the current match with the given text and advances to the next match.

public bool Replace(string replacement)

Parameters

replacement string

The replacement text.

Returns

bool

True if a replacement was made, false if no current match or read-only.

ReplaceAll(string)

Replaces all search matches with the given text.

public int ReplaceAll(string replacement)

Parameters

replacement string

The replacement text.

Returns

int

The number of replacements made.

SelectRange(int, int, int, int)

Selects the range (startLine, startCol) to (endLine, endCol), 0-based. Moves cursor to the end of the selection and scrolls it into view.

public void SelectRange(int startLine, int startCol, int endLine, int endCol)

Parameters

startLine int
startCol int
endLine int
endCol int

SetContent(string)

Sets the text content from a string, splitting on line breaks.

public void SetContent(string content)

Parameters

content string

The text content to set.

SetContentLines(List<string>)

Sets the content of the control using a list of strings, with each string representing a line.

public void SetContentLines(List<string> lines)

Parameters

lines List<string>

The lines to set as content.

SetLineHighlight(int, Color?)

Sets or clears a background highlight for a specific source line. Pass null to clear the highlight for that line.

public void SetLineHighlight(int sourceLineIndex, Color? color)

Parameters

sourceLineIndex int
color Color?

SetLogicalCursorPosition(Point)

Sets the logical cursor position within the control's content coordinate system.

public void SetLogicalCursorPosition(Point position)

Parameters

position Point

Events

ContentChanged

Occurs when the text content changes.

public event EventHandler<string>? ContentChanged

Event Type

EventHandler<string>

CursorPositionChanged

Occurs when the cursor position changes. Event argument is a tuple of (Line, Column) using 1-based indices.

public event EventHandler<(int Line, int Column)>? CursorPositionChanged

Event Type

EventHandler<(int Line, int Column)>

EditingModeChanged

Occurs when the editing mode changes (entering or leaving edit mode). Event argument is true when entering edit mode, false when leaving.

public event EventHandler<bool>? EditingModeChanged

Event Type

EventHandler<bool>

GutterClick

Occurs when the user clicks on the gutter area (left of the text).

public event EventHandler<GutterClickEventArgs>? GutterClick

Event Type

EventHandler<GutterClickEventArgs>

MatchCountChanged

Occurs when the search match count changes (after Find, Replace, ReplaceAll, or content changes). Event argument is the new match count.

public event EventHandler<int>? MatchCountChanged

Event Type

EventHandler<int>

MouseClick

Event fired when the control is clicked

public event EventHandler<MouseEventArgs>? MouseClick

Event Type

EventHandler<MouseEventArgs>

MouseDoubleClick

Event fired when the control is double-clicked

public event EventHandler<MouseEventArgs>? MouseDoubleClick

Event Type

EventHandler<MouseEventArgs>

MouseEnter

Event fired when the mouse enters the control area

public event EventHandler<MouseEventArgs>? MouseEnter

Event Type

EventHandler<MouseEventArgs>

MouseLeave

Event fired when the mouse leaves the control area

public event EventHandler<MouseEventArgs>? MouseLeave

Event Type

EventHandler<MouseEventArgs>

MouseMove

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

OverwriteModeChanged

Occurs when overwrite mode is toggled. Event argument is true when overwrite mode is active, false for insert mode.

public event EventHandler<bool>? OverwriteModeChanged

Event Type

EventHandler<bool>

SelectionChanged

Occurs when the selection state changes (selected/deselected or bounds change). Event argument is the currently selected text, or empty string if no selection.

public event EventHandler<string>? SelectionChanged

Event Type

EventHandler<string>