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()Public Sub 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();
}Private Sub htmlEditor1_DocumentReady(sender As Object, e As EventArgs)
' Make the editor large enough to show the whole document at once.
htmlEditor1.EditorService.AdjustEditorSize()
End SubAdjustEditorWidth 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()Public Sub AdjustEditorWidth()Example
private void widenButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.AdjustEditorWidth();
}Private Sub widenButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.AdjustEditorWidth()
End SubAdjustEditorHeight 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()Public Sub AdjustEditorHeight()Example
private void heightFitButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.AdjustEditorHeight();
}Private Sub heightFitButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.AdjustEditorHeight()
End SubGetScrollBarSetting 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()Public Function GetScrollBarSetting() As ScrollBarVisibilityReturns
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)Public Sub SetScrollBarSetting(scrollbarValue As ScrollBarVisibility)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()Public Function GetScrollPosition() As PointReturns
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)Public Sub SetScrollPosition(x As Integer, y As Integer)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()Public Function IsMsWordContentInClipboard() As BooleanReturns
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()Public Sub PasteFromMsWord()Example
if (htmlEditor1.EditorService.IsMsWordContentInClipboard())
htmlEditor1.EditorService.PasteFromMsWord();
else
htmlEditor1.EditorService.Paste();If htmlEditor1.EditorService.IsMsWordContentInClipboard() Then
htmlEditor1.EditorService.PasteFromMsWord()
Else
htmlEditor1.EditorService.Paste()
End IfCut 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()Public Sub Cut()Example
private void cutToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.Cut();
}Private Sub cutToolStripButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.Cut()
End SubCopy Method
Copies the current selection to the clipboard without modifying the document. Equivalent to pressing Ctrl+C.
Syntax
public void Copy()Public Sub Copy()Example
private void copyToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.Copy();
}Private Sub copyToolStripButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.Copy()
End SubDelete 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()Public Sub Delete()Example
private void deleteToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.Delete();
}Private Sub deleteToolStripButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.Delete()
End SubPaste Method
Pastes the current clipboard content at the caret position. For Word-sourced content, prefer PasteFromMsWord which produces cleaner markup.
Syntax
public void Paste()Public Sub Paste()Example
private void pasteToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.Paste();
}Private Sub pasteToolStripButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.Paste()
End SubUndo Method
Reverses the most recent edit. Calls are no-ops when the undo stack is empty.
Syntax
public void Undo()Public Sub Undo()Example
private void undoToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.Undo();
}Private Sub undoToolStripButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.Undo()
End SubRedo Method
Re-applies the most recent edit that was undone. Calls are no-ops when the redo stack is empty.
Syntax
public void Redo()Public Sub Redo()Example
private void redoToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.Redo();
}Private Sub redoToolStripButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.Redo()
End SubToggleOverWriteMode 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()Public Sub ToggleOverWriteMode()Example
private void insertKey_Pressed(object sender, EventArgs e)
{
htmlEditor1.EditorService.ToggleOverWriteMode();
}Private Sub insertKey_Pressed(sender As Object, e As EventArgs)
htmlEditor1.EditorService.ToggleOverWriteMode()
End SubPrint 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)Public Sub Print(showDialog As Boolean)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);
}Private Sub printToolStripButton_Click(sender As Object, e As EventArgs)
' Show the Print dialog so the user can choose printer and copies.
htmlEditor1.EditorService.Print(showDialog:=True)
End SubExecCommand 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)Public Sub ExecCommand(cmdId As String, Optional showUi As Boolean = False, Optional value As Object? = Nothing)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/>");' 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()Public Sub ShowPrintPreviewDialog()Example
private void printPreviewToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.ShowPrintPreviewDialog();
}Private Sub printPreviewToolStripButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.ShowPrintPreviewDialog()
End SubShowPageSetupDialog Method
Opens the browser Page Setup dialog so the user can change margins, orientation, headers and footers used when printing.
Syntax
public void ShowPageSetupDialog()Public Sub ShowPageSetupDialog()Example
private void pageSetupToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.ShowPageSetupDialog();
}Private Sub pageSetupToolStripButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.ShowPageSetupDialog()
End SubJustifyLeft 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()Public Sub JustifyLeft()Example
private void alignLeftToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.JustifyLeft();
}Private Sub alignLeftToolStripButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.JustifyLeft()
End SubJustifyRight Method
Toggles right justification on the block element containing the caret or selection.
Syntax
public void JustifyRight()Public Sub JustifyRight()Example
private void alignRightToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.JustifyRight();
}Private Sub alignRightToolStripButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.JustifyRight()
End SubJustifyCenter Method
Toggles center justification on the block element containing the caret or selection.
Syntax
public void JustifyCenter()Public Sub JustifyCenter()Example
private void alignCenterToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.JustifyCenter();
}Private Sub alignCenterToolStripButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.JustifyCenter()
End SubSearch 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)Public Function Search(text As String, forward As Boolean, matchWholeWord As Boolean, matchCase As Boolean, Optional useCaretPosition As Boolean = False) As BooleanParameters
| 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.");Dim found As Boolean = htmlEditor1.EditorService.Search(text:="TODO", forward:=True, matchWholeWord:=False, matchCase:=False)
If Not found Then 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)Public Function Replace(searchString As String, replacement As String, matchWholeWord As Boolean, matchCase As Boolean) As BooleanParameters
| 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);
}' Interactive workflow: Find, then Replace on user click.
If htmlEditor1.EditorService.Search("colour", True, False, False) Then
htmlEditor1.EditorService.Replace(searchString:="colour", replacement:="color", matchWholeWord:=False, matchCase:=False)
End IfReplaceAll 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)Public Function ReplaceAll(replaceWhat As String, replaceWith As String, matchWholeWord As Boolean, matchCase As Boolean) As IntegerParameters
| 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).");Dim count As Integer = 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()Public Sub SearchWithDefaultIeDialog()Example
private void findToolStripButton_Click(object sender, EventArgs e)
{
htmlEditor1.EditorService.SearchWithDefaultIeDialog();
}Private Sub findToolStripButton_Click(sender As Object, e As EventArgs)
htmlEditor1.EditorService.SearchWithDefaultIeDialog()
End SubDeleteText 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)Public Sub DeleteText(startIndex As Integer, length As Integer)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)Public Sub ReplaceText(startIndex As Integer, length As Integer, replacementText As String)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)Public Sub PrintPageSetup(margin As Double)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);' 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)Public Sub PrintPageSetup(topMargin As Double, bottomMargin As Double, rightMargin As Double, leftMargin As Double)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()Public Sub 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; }Public ReadOnly Property HasHorizontalScrollbar As BooleanReturns
System.Boolean - true if a horizontal scrollbar is currently visible.
Example
if (htmlEditor1.EditorService.HasHorizontalScrollbar)
htmlEditor1.EditorService.AdjustEditorWidth(); // grow to fitIf htmlEditor1.EditorService.HasHorizontalScrollbar Then htmlEditor1.EditorService.AdjustEditorWidth() ' grow to fitHasVerticalScrollbar Property
Indicates whether the editor is currently displaying a vertical scrollbar. Read-only.
Syntax
public bool HasVerticalScrollbar { get; }Public ReadOnly Property HasVerticalScrollbar As BooleanReturns
System.Boolean - true if a vertical scrollbar is currently visible.
Example
if (htmlEditor1.EditorService.HasVerticalScrollbar)
htmlEditor1.EditorService.AdjustEditorHeight();If htmlEditor1.EditorService.HasVerticalScrollbar Then 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; }Public Property NoPrinterMessageCaption As StringReturns
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";