Selection Service

The ISelectionService interface exposes selection-related operations for a WinFormHtmlEditor control. It is reached through the Selection property on the editor: htmlEditor1.Selection.SelectAll(). Methods cover programmatic text selection (by index range, by search text, or by HTML element), inspection of what is currently selected, and helpers for whitespace trimming and length queries.

The interface implements IDisposable because the runtime implementation holds a reference to the underlying MSHTML selection object. The editor disposes the service when the control itself is disposed, so application code never needs to call Dispose directly.

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

SelectText(Int32, Int32) Method

Selects a range of characters in the document body, starting at startIndex and continuing for length characters. Use this overload when you already know the exact extents of the range to highlight.

Syntax
void SelectText(int startIndex, int length);
Parameters
NameTypeDescription
startIndexInt32Zero-based character offset within the body where the selection begins.
lengthInt32Number of characters to include in the selection.
Returns

None.

Remarks

Indexes count characters in the rendered text stream, not bytes in the raw HTML source. Call htmlEditor1.Selection.GetSelectionLength() afterwards to confirm the highlighted range.

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

htmlEditor1.Focus();

htmlEditor1.Selection.SelectText(0, 12);

Debug.WriteLine($"Selected: {htmlEditor1.Selection.GetSelectedText()}");

SelectText(Int32) Method

Selects from startIndex to the end of the body. Useful for "select to end" affordances such as Shift+End in a paragraph.

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

None.

Example
// Select everything from the 20th character to the end of the body.

htmlEditor1.Selection.SelectText(20);

int len = htmlEditor1.Selection.GetSelectionLength();

Debug.WriteLine($"Selected {len} characters to end of body.");

SelectText(String, Boolean, Boolean, Boolean) Method

Searches the document body for text and, if found, selects the first match. Supports forward and backward search, whole-word matching, and case-sensitive matching - the same options offered by a typical Find dialog.

Syntax
bool SelectText(string text, bool forward, bool matchWholeWord, bool matchCase);
Parameters
NameTypeDescription
textStringThe text to search for.
forwardBooleantrue to search forward from the current caret position; false to search backward.
matchWholeWordBooleantrue to require word-boundary matches; false to allow matches inside larger words.
matchCaseBooleantrue for case-sensitive matching; false for case-insensitive.
Returns

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

Example
bool found = htmlEditor1.Selection.SelectText("Contoso",

    forward: true, matchWholeWord: true, matchCase: false);

if (!found)

{

    MessageBox.Show("Not found");

}

SelectHtmlElement(IHTMLElement) Method

Selects the entire HTML element passed in, including all of its descendants. The selection visibly highlights the element's rendered area.

Syntax
void SelectHtmlElement(IHTMLElement element);
Parameters
NameTypeDescription
elementmshtml.IHTMLElementThe MSHTML element to select. Typically obtained from htmlEditor1.StateQuery.GetActiveHtmlElement() or from a DOM walk.
Returns

None.

Example
// Highlight the element under the caret.

IHTMLElement active = htmlEditor1.StateQuery.GetActiveHtmlElement();

if (active != null)

{

    htmlEditor1.Selection.SelectHtmlElement(active);

}

GetSelectionInfo Method

Returns a snapshot of the current selection, including its zero-based start index, length, plain text, and inner HTML.

Syntax
SelectionInfo GetSelectionInfo();
Returns

A SelectionInfo instance with these public fields:

FieldTypeDescription
StartIndexInt32Zero-based offset of the selection start within the body.
LengthInt32Length of the selection in characters.
TextStringThe selected text (no markup).
HtmlStringThe selected HTML fragment.
Example
SelectionInfo info = htmlEditor1.Selection.GetSelectionInfo();

Debug.WriteLine($"{info.StartIndex}+{info.Length}: {info.Text}");

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

DeselectAll Method

Clears the current selection so that no HTML elements are selected in the editor. Equivalent in spirit to TextBox.DeselectAll() on a .NET TextBox: the caret remains visible, but nothing is highlighted.

Syntax
void DeselectAll();
Returns

None.

Example
// Drop any active selection after applying a programmatic change.

htmlEditor1.Selection.SelectAll();

htmlEditor1.Formatting.SetFontSize(12);

htmlEditor1.Selection.DeselectAll();

Unselect Method

Removes the current selection. Functionally a synonym for DeselectAll; retained for parity with the underlying MSHTML unselect command.

Syntax
void Unselect();
Returns

None.

Example
// Same effect as DeselectAll. Kept for source compatibility.

htmlEditor1.Selection.Unselect();

SelectAll Method

Selects the entire document body. Equivalent to pressing Ctrl+A while the editor has focus.

Syntax
void SelectAll();
Returns

None.

Example
// Select everything, then read the plain text.

htmlEditor1.Selection.SelectAll();

string body = htmlEditor1.Selection.GetSelectedText();

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

GetSelectedHtml Method

Returns the HTML fragment that represents the current selection, including all inline tags inside the range.

Syntax
string GetSelectedHtml();
Returns

The selected HTML as a string. An empty string is returned when nothing is selected.

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

if (!string.IsNullOrEmpty(html))

{

    Clipboard.SetText(html);

}

GetSelectedText Method

Returns the plain text of the current selection, with all HTML markup stripped.

Syntax
string GetSelectedText();
Returns

The selected text as a string. An empty string is returned when nothing is selected.

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

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

TrimSelection(TrimDirections) Method

Trims whitespace from the start, the end, or both ends of the current selection, then returns the trimmed text. Useful when a user double-clicks a word that picked up trailing whitespace and you want a clean token.

Syntax
string TrimSelection(TrimDirections trimDirection = TrimDirections.Both);
Parameters
NameTypeDescription
trimDirectionTrimDirectionsStart trims leading whitespace, End trims trailing whitespace, Both (default) trims both ends.
Returns

The trimmed selection text as a string.

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

string clean = htmlEditor1.Selection.TrimSelection(TrimDirections.Both);

Debug.WriteLine($"Clean token: '{clean}'");

GetSelectionLength Method

Returns the length, in characters, of the current selection. Returns 0 when nothing is selected.

Syntax
int GetSelectionLength();
Returns

Integer character count of the current selection.

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

{

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

    return;

}

Dispose Method

Inherited from IDisposable. Called by the editor control during teardown so it can release the underlying MSHTML selection object. Host applications normally do not invoke this directly.

Syntax
void Dispose();
Returns

None.

Example
// Disposing the editor disposes its services automatically.

htmlEditor1.Dispose();

// Equivalent of explicitly disposing the selection service - not normally needed:

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