Selection Service

The Selection Service is the programmatic gateway to the WPF HTML editor's current text selection. It lets host code position the caret, highlight character ranges, search-and-select substrings, retrieve what the user has selected, and clear the selection - without depending on focus or simulated keystrokes. The service is reached through the MyEditor.Selection property and implements ISelectionService.

Indexes are zero-based and measure characters in the rendered text stream, not bytes in the HTML source. The companion SelectionInfo object returned by GetSelectionInfo() bundles the start index, length, plain text and HTML of the current selection in a single object, which is the recommended way to snapshot selection state.

Calling a selection method does not change formatting - combine these methods with the Formatting Service when you want to make a structural change and apply formatting to a precise range in a single operation.

Namespace: SpiceLogic.HtmlEditor.Abstractions.Services
Assembly: SpiceLogic.HtmlEditor.Abstractions
Inheritance: ISelectionService : IDisposable

SelectText(Int32, Int32) Method

Selects a contiguous range of characters starting at startIndex and spanning length characters.

Syntax
void SelectText(int startIndex, int length);
Parameters
NameTypeDescription
startIndexInt32Zero-based character index where the selection begins.
lengthInt32Number of characters to select.
Returns

None.

Example
// Select the first 12 characters of the document.

MyEditor.Focus();

MyEditor.Selection.SelectText(0, 12);

SelectText(Int32) Method

Selects from the given start index to the end of the document body.

Syntax
void SelectText(int startIndex);
Parameters
NameTypeDescription
startIndexInt32Zero-based character index where the selection begins.
Returns

None.

Example
// Select from character 50 to the very end of the body.

MyEditor.Selection.SelectText(50);

Debug.WriteLine($"Tail length: {MyEditor.Selection.GetSelectionLength()}");

SelectText(String, Boolean, Boolean, Boolean) Method

Searches the document for the specified text and, if found, selects the first match in the given direction. This is the same primitive the built-in Find dialog is built on.

Syntax
bool SelectText(string text, bool forward, bool matchWholeWord, bool matchCase);
Parameters
NameTypeDescription
textStringThe search term.
forwardBooleantrue to search from the caret toward the end of the document; false to search backward.
matchWholeWordBooleantrue to require word-boundary matches.
matchCaseBooleantrue for case-sensitive matching.
Returns

true if a match was found and selected; false otherwise.

Example
if (!MyEditor.Selection.SelectText("TODO", forward: true,

        matchWholeWord: true, matchCase: false))

{

    MessageBox.Show("No more TODO markers.");

}

SelectHtmlElement(IHTMLElement) Method

Selects the specified MSHTML element, expanding the selection to cover the entire element. Useful in conjunction with MyEditor.StateQuery.GetActiveHtmlElement() or GetElementFromPoint() to highlight an element identified by some other route (for example a hover or right-click).

Syntax
void SelectHtmlElement(IHTMLElement element);
Parameters
NameTypeDescription
elementmshtml.IHTMLElementThe HTML element to select.
Returns

None.

Example
IHTMLElement active = MyEditor.StateQuery.GetActiveHtmlElement();

if (active != null)

{

    MyEditor.Selection.SelectHtmlElement(active);

}

GetSelectionInfo Method

Returns a snapshot of the current selection. SelectionInfo bundles the start index, length, plain text and HTML in one allocation, which is cheaper than calling the individual getters when you need more than one value.

Syntax
SelectionInfo GetSelectionInfo();
Returns

A populated SelectionInfo with StartIndex, Length, Text, and Html. For an empty selection, Length is 0 and the text and html fields are empty strings.

Example
var info = MyEditor.Selection.GetSelectionInfo();

Debug.WriteLine($"Selected {info.Length} chars: {info.Text}");

Debug.WriteLine($"HTML: {info.Html}");

DeselectAll Method

Clears any active selection without altering the caret position. Equivalent in spirit to TextBox.DeselectAll in classic WinForms / WPF text controls.

Syntax
void DeselectAll();
Returns

None.

Example
// Drop the highlight after a programmatic update.

MyEditor.Selection.SelectAll();

MyEditor.Formatting.SetFontSize(12);

MyEditor.Selection.DeselectAll();

Unselect Method

Clears any active selection. Synonym for DeselectAll, retained for backward compatibility with earlier editor versions.

Syntax
void Unselect();
Returns

None.

Example
// Same effect as DeselectAll; provided for source compatibility.

MyEditor.Selection.Unselect();

SelectAll Method

Selects the entire document body. Equivalent to the user pressing Ctrl+A inside the editor.

Syntax
void SelectAll();
Returns

None.

Example
MyEditor.Selection.SelectAll();

string body = MyEditor.Selection.GetSelectedText();

Debug.WriteLine($"Body text length: {body.Length}");

GetSelectedHtml Method

Returns the HTML markup that backs the current selection. Markup is returned as the editor sees it, including inline styles and any wrapping elements (<span>, <b>, etc.).

Syntax
string GetSelectedHtml();
Returns

The selected HTML fragment, or String.Empty when there is no active selection.

Example
string html = MyEditor.Selection.GetSelectedHtml();

if (!string.IsNullOrEmpty(html))

{

    Clipboard.SetText(html);

}

GetSelectedText Method

Returns the plain-text content of the current selection with HTML tags stripped.

Syntax
string GetSelectedText();
Returns

The selected text, or String.Empty when there is no active selection.

Example
string text = MyEditor.Selection.GetSelectedText();

StatusText.Text = $"Selected: {text}";

TrimSelection(TrimDirections) Method

Shrinks the active selection by removing leading and / or trailing whitespace characters. Returns the trimmed text. Particularly useful after SelectText(string, ...) matches that pulled in surrounding spaces, or after the user double-clicks to select a word and the platform extended the selection a character too far.

Syntax
string TrimSelection(TrimDirections trimDirection = TrimDirections.Both);
Parameters
NameTypeDescription
trimDirectionTrimDirectionsOne of Start, End, or Both (default).
Returns

The trimmed selection text.

Example
MyEditor.Selection.SelectText("  hello world  ", true, false, true);

string clean = MyEditor.Selection.TrimSelection(); // -> "hello world"

Debug.WriteLine(clean);

GetSelectionLength Method

Returns the number of characters in the current selection.

Syntax
int GetSelectionLength();
Returns

The selection length in characters; 0 when nothing is selected.

Example
if (MyEditor.Selection.GetSelectionLength() == 0)

{

    MessageBox.Show("Please select some text first.");

    return;

}

Dispose Method

Inherited from IDisposable. Called by the editor control during teardown - host applications normally do not invoke this directly.

Syntax
void Dispose();
Returns

None.

Example
// Disposing the editor disposes its services automatically.

MyEditor.Dispose();

// Explicit form is rarely needed:

// MyEditor.Selection.Dispose();
Last updated on May 14, 2026