Editor Services
The IEditorService interface, exposed through the WinFormHtmlEditor.EditorService property, is the workhorse API for the editor. It bundles together clipboard commands, undo and redo, find and replace, scrollbar layout, justification, print and page setup, and the low-level ExecCommand dispatcher. Most user-facing toolbar buttons in the bundled sample applications are thin wrappers over methods on this service.
The service is owned by the editor and disposed when the editor is disposed; do not dispose it manually. Calls must happen after the document is ready - the simplest way to guarantee that is to call methods from event handlers raised by the editor itself (for example, WinFormHtmlEditor.DocumentReady).
Search and Replace methods operate against the rendered text and return success flags or counts you can surface in your UI. The ExecCommand method is an escape hatch into the underlying browser command engine and accepts any of the documented MSHTML execCommand identifiers. In all examples below, htmlEditor1 is a WinFormHtmlEditor placed on the form at design time.
AdjustEditorSize Method
Resizes both the width and the height of the editor so the entire document fits without needing scrollbars. Useful for "auto-grow" layouts where the editor should expand to its content rather than scroll.
Syntax
public void AdjustEditorSize()Example
private void htmlEditor1_DocumentReady(object sender, EventArgs e)
{
// Make the editor large enough to show the whole document at once.
htmlEditor1.EditorService.AdjustEditorSize();
}AdjustEditorWidth Method
Resets the control width so the horizontal scrollbar is not needed; the editor expands or shrinks based on the widest visible content. Height is left unchanged.
Syntax
public void AdjustEditorWidth()Example
private void widenButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.AdjustEditorWidth();
}AdjustEditorHeight Method
Resets the control height so the vertical scrollbar is not needed; the editor expands or shrinks based on visible content. Width is left unchanged.
Syntax
public void AdjustEditorHeight()Example
private void heightFitButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.AdjustEditorHeight();
}GetScrollBarSetting Method
Returns the current scrollbar visibility policy as a ScrollBarVisibility enum value. Useful when you need to toggle scrollbars and want to restore the prior setting.
Syntax
public ScrollBarVisibility GetScrollBarSetting()Returns
ScrollBarVisibility — the current scrollbar policy.
Example
ScrollBarVisibility prior = htmlEditor1.EditorService.GetScrollBarSetting();
htmlEditor1.EditorService.SetScrollBarSetting(ScrollBarVisibility.None);
PrintEditor();
htmlEditor1.EditorService.SetScrollBarSetting(prior); // restoreSetScrollBarSetting Method
Applies a scrollbar visibility policy to the editor. The policy controls whether each scrollbar is always shown, never shown, shown only when needed, or driven by the document body style.
Syntax
public void SetScrollBarSetting(ScrollBarVisibility scrollbarValue)Parameters
| Name | Type | Description |
|---|---|---|
| scrollbarValue | ScrollBarVisibility | One of Auto, Both, Horizontal, Vertical, None, Default, or Custom. |
Remarks
Choose Custom when you want full control through the BodyStyle CSS overflow attribute - the editor will not overwrite your custom value in that mode.
Example
// Show scrollbars only when content overflows.
htmlEditor1.EditorService.SetScrollBarSetting(ScrollBarVisibility.Auto);GetScrollPosition Method
Returns the current scroll offset of the editor viewport as a System.Drawing.Point. X is the horizontal scroll position and Y is the vertical scroll position, both in pixels.
Syntax
public Point GetScrollPosition()Returns
System.Drawing.Point — the current scroll offset in pixels.
Example
Point savedScroll = htmlEditor1.EditorService.GetScrollPosition();
htmlEditor1.LoadHtml(updatedHtml);
htmlEditor1.EditorService.SetScrollPosition(savedScroll.X, savedScroll.Y);SetScrollPosition Method
Scrolls the editor viewport to the absolute pixel coordinates supplied. Coordinates outside the scrollable range are clamped. Combine with GetScrollPosition to save and restore scroll state across page reloads.
Syntax
public void SetScrollPosition(int x, int y)Parameters
| Name | Type | Description |
|---|---|---|
| x | System.Int32 | Horizontal scroll position in pixels. |
| y | System.Int32 | Vertical scroll position in pixels. |
Example
// Jump to the top of the document.
htmlEditor1.EditorService.SetScrollPosition(0, 0);IsMsWordContentInClipboard Method
Determines whether the Windows clipboard currently holds content that originated from Microsoft Word. Use this to decide whether to enable a "Paste from Word" UI affordance.
Syntax
public bool IsMsWordContentInClipboard()Returns
System.Boolean — true if the clipboard holds Word content; otherwise false.
Example
pasteFromWordToolStripButton.Enabled =
htmlEditor1.EditorService.IsMsWordContentInClipboard();PasteFromMsWord Method
Pastes clipboard content into the editor while stripping Word-specific noise (mso classes, conditional comments, redundant span wrappers). Produces cleaner HTML than a raw Paste when the source is Word.
Syntax
public void PasteFromMsWord()Example
if (htmlEditor1.EditorService.IsMsWordContentInClipboard())
htmlEditor1.EditorService.PasteFromMsWord();
else
htmlEditor1.EditorService.Paste();Cut Method
Performs a clipboard Cut: the current selection is copied to the clipboard and removed from the document. Equivalent to pressing Ctrl+X.
Syntax
public void Cut()Example
private void cutToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.Cut();
}Copy Method
Copies the current selection to the clipboard without modifying the document. Equivalent to pressing Ctrl+C.
Syntax
public void Copy()Example
private void copyToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.Copy();
}Delete Method
Deletes the current selection in the editor. If there is no selection, deletes the character to the right of the caret.
Syntax
public void Delete()Example
private void deleteToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.Delete();
}Paste Method
Pastes the current clipboard content at the caret position. For Word-sourced content, prefer PasteFromMsWord which produces cleaner markup.
Syntax
public void Paste()Example
private void pasteToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.Paste();
}Undo Method
Reverses the most recent edit. Calls are no-ops when the undo stack is empty.
Syntax
public void Undo()Example
private void undoToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.Undo();
}Redo Method
Re-applies the most recent edit that was undone. Calls are no-ops when the redo stack is empty.
Syntax
public void Redo()Example
private void redoToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.Redo();
}ToggleOverWriteMode Method
Toggles between insert mode and overwrite mode for the editor. In overwrite mode, typing replaces the character to the right of the caret instead of inserting before it.
Syntax
public void ToggleOverWriteMode()Example
private void insertKey_Pressed(object sender, EventArgs e)
{
htmlEditor1.EditorService.ToggleOverWriteMode();
}Print Method
Prints the editor's HTML content. When showDialog is true the standard Print dialog is shown so the user can pick printer and copies; otherwise the document is sent directly to the default printer.
Syntax
public void Print(bool showDialog)Parameters
| Name | Type | Description |
|---|---|---|
| showDialog | System.Boolean | If true, prompts the user with the Print dialog before printing. |
Remarks
If no printer is installed a message box is shown using the caption from NoPrinterMessageCaption.
Example
private void printToolStripButton_Click(object sender, EventArgs e)
{
// Show the Print dialog so the user can choose printer and copies.
htmlEditor1.EditorService.Print(showDialog: true);
}ExecCommand Method
Dispatches an arbitrary command to the underlying browser command engine. This is the escape hatch for advanced scenarios where the strongly typed services do not expose what you need. Identifiers follow the legacy MSHTML execCommand set (for example "InsertHTML", "FontName", "ForeColor").
Syntax
public void ExecCommand(string cmdId, bool showUi = false, object? value = null)Parameters
| Name | Type | Description |
|---|---|---|
| cmdId | System.String | Browser command identifier, for example "InsertHTML". |
| showUi | System.Boolean | If true, requests that the browser display any UI associated with the command. Default false. |
| value | System.Object | Command-specific argument; pass null when not required. |
Example
// Insert a horizontal rule at the caret using the underlying engine.
htmlEditor1.EditorService.ExecCommand("InsertHTML", false, "<hr/>");ShowPrintPreviewDialog Method
Opens the browser Print Preview dialog so the user can review pagination and print settings before printing.
Syntax
public void ShowPrintPreviewDialog()Example
private void printPreviewToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.ShowPrintPreviewDialog();
}ShowPageSetupDialog Method
Opens the browser Page Setup dialog so the user can change margins, orientation, headers and footers used when printing.
Syntax
public void ShowPageSetupDialog()Example
private void pageSetupToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.ShowPageSetupDialog();
}JustifyLeft Method
Toggles left justification on the block element containing the caret or selection. Calling it again on an already-left-justified block removes the alignment.
Syntax
public void JustifyLeft()Example
private void alignLeftToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.JustifyLeft();
}JustifyRight Method
Toggles right justification on the block element containing the caret or selection.
Syntax
public void JustifyRight()Example
private void alignRightToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.JustifyRight();
}JustifyCenter Method
Toggles center justification on the block element containing the caret or selection.
Syntax
public void JustifyCenter()Example
private void alignCenterToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.JustifyCenter();
}Search Method
Searches for text within the editor and selects the next match. The search direction, whole-word matching, and case sensitivity are all caller-controlled. When useCaretPosition is true and there is no current selection, the search begins at the caret rather than at the document origin.
Syntax
public bool Search(string text, bool forward, bool matchWholeWord, bool matchCase, bool useCaretPosition = false)Parameters
| Name | Type | Description |
|---|---|---|
| text | System.String | The text to search for. |
| forward | System.Boolean | If true, search forward from the current position; otherwise search backward. |
| matchWholeWord | System.Boolean | If true, match only when the candidate is a whole word. |
| matchCase | System.Boolean | If true, the comparison is case sensitive. |
| useCaretPosition | System.Boolean | If true and the selection is empty, search from the caret position. Default false. |
Returns
System.Boolean — true if a match was found and selected; otherwise false.
Example
bool found = htmlEditor1.EditorService.Search(
text: "TODO",
forward: true,
matchWholeWord: false,
matchCase: false);
if (!found)
MessageBox.Show("No more matches.");Replace Method
Replaces the currently selected text with replacement if (and only if) the selection equals searchString under the supplied matching options. Use this for "Find next, then Replace" interactive workflows.
Syntax
public bool Replace(string searchString, string replacement, bool matchWholeWord, bool matchCase)Parameters
| Name | Type | Description |
|---|---|---|
| searchString | System.String | The text the current selection must equal for the replacement to occur. |
| replacement | System.String | The replacement text. |
| matchWholeWord | System.Boolean | If true, the selection must be a whole-word match. |
| matchCase | System.Boolean | If true, comparison is case sensitive. |
Returns
System.Boolean — true if the selection was replaced; otherwise false.
Example
// Interactive workflow: Find, then Replace on user click.
if (htmlEditor1.EditorService.Search("colour", true, false, false))
{
htmlEditor1.EditorService.Replace(
searchString: "colour",
replacement: "color",
matchWholeWord: false,
matchCase: false);
}ReplaceAll Method
Replaces every occurrence of replaceWhat in the document with replaceWith. The returned count tells you how many replacements were applied so you can surface a "Replaced N occurrences" message.
Syntax
public int ReplaceAll(string replaceWhat, string replaceWith, bool matchWholeWord, bool matchCase)Parameters
| Name | Type | Description |
|---|---|---|
| replaceWhat | System.String | The text to find. |
| replaceWith | System.String | The text to substitute for each match. |
| matchWholeWord | System.Boolean | If true, only whole-word matches are replaced. |
| matchCase | System.Boolean | If true, comparison is case sensitive. |
Returns
System.Int32 — the number of occurrences that were replaced.
Example
int count = htmlEditor1.EditorService.ReplaceAll(
replaceWhat: "color",
replaceWith: "colour",
matchWholeWord: false,
matchCase: false);
MessageBox.Show($"Replaced {count} occurrence(s).");SearchWithDefaultIeDialog Method
Pops up the browser's built-in Find dialog inside the editor surface. Useful when you want to defer Find UI to the browser instead of building your own.
Syntax
public void SearchWithDefaultIeDialog()Example
private void findToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.SearchWithDefaultIeDialog();
}DeleteText Method
Deletes length characters of rendered text starting at startIndex. The caret is collapsed to the deletion point afterwards. Use when you need surgical, index-based removal without selecting first.
Syntax
public void DeleteText(int startIndex, int length)Parameters
| Name | Type | Description |
|---|---|---|
| startIndex | System.Int32 | Zero-based starting offset into the rendered text. |
| length | System.Int32 | Number of characters to delete. |
Example
// Remove the first 10 characters of plain text.
htmlEditor1.EditorService.DeleteText(startIndex: 0, length: 10);ReplaceText Method
Replaces length characters of rendered text starting at startIndex with replacementText. Equivalent to a DeleteText followed by an insertion, but performed atomically so a single undo step reverses the change.
Syntax
public void ReplaceText(int startIndex, int length, string replacementText)Parameters
| Name | Type | Description |
|---|---|---|
| startIndex | System.Int32 | Zero-based starting offset into the rendered text. |
| length | System.Int32 | Number of characters to replace. |
| replacementText | System.String | Text to substitute for the removed range. |
Example
// Replace the 4 characters starting at offset 12 with a new word.
htmlEditor1.EditorService.ReplaceText(12, 4, "Hello");PrintPageSetup Method (Uniform Margin)
Sets all four print margins to the same value, in inches. Convenient when you want uniform margins without specifying each side.
Syntax
public void PrintPageSetup(double margin)Parameters
| Name | Type | Description |
|---|---|---|
| margin | System.Double | Margin value in inches, applied to top, bottom, left and right. |
Example
// One-inch margins on all four sides.
htmlEditor1.EditorService.PrintPageSetup(margin: 1.0);
htmlEditor1.EditorService.Print(showDialog: false);PrintPageSetup Method (Per-Side Margins)
Sets each print margin individually, in inches. Use this overload when the four margins differ - for example, a wider left margin for binding.
Syntax
public void PrintPageSetup(double topMargin, double bottomMargin, double rightMargin, double leftMargin)Parameters
| Name | Type | Description |
|---|---|---|
| topMargin | System.Double | Top margin in inches. |
| bottomMargin | System.Double | Bottom margin in inches. |
| rightMargin | System.Double | Right margin in inches. |
| leftMargin | System.Double | Left margin in inches. |
Example
// Wider left margin to leave room for binding.
htmlEditor1.EditorService.PrintPageSetup(
topMargin: 1.0,
bottomMargin: 1.0,
rightMargin: 0.75,
leftMargin: 1.25);ClearUndoStack Method
Discards the entire undo history. Subsequent Undo calls will be no-ops until new edits are recorded. Useful after loading a fresh document to ensure the user cannot "undo back into" the previous document's state.
Syntax
public void ClearUndoStack()Example
// After loading a brand-new document, prevent undo across documents.
htmlEditor1.LoadHtml(freshHtml);
htmlEditor1.EditorService.ClearUndoStack();HasHorizontalScrollbar Property
Indicates whether the editor is currently displaying a horizontal scrollbar. Read-only; the value reflects layout, not policy - it can be false even when SetScrollBarSetting was called with Both if the content fits horizontally.
Syntax
public bool HasHorizontalScrollbar { get; }Returns
System.Boolean — true if a horizontal scrollbar is currently visible.
Example
if (htmlEditor1.EditorService.HasHorizontalScrollbar)
htmlEditor1.EditorService.AdjustEditorWidth(); // grow to fitHasVerticalScrollbar Property
Indicates whether the editor is currently displaying a vertical scrollbar. Read-only.
Syntax
public bool HasVerticalScrollbar { get; }Returns
System.Boolean — true if a vertical scrollbar is currently visible.
Example
if (htmlEditor1.EditorService.HasVerticalScrollbar)
htmlEditor1.EditorService.AdjustEditorHeight();NoPrinterMessageCaption Property
Gets or sets the caption used for the message box shown when Print is invoked and no printer is installed. Localize this string at startup if your application targets non-English locales.
Syntax
public string NoPrinterMessageCaption { get; set; }Returns
System.String — the caption of the "no printer" message box.
Example
// Localise the caption shown when no printer is installed.
htmlEditor1.EditorService.NoPrinterMessageCaption = "Printer not available";