Inserting HTML at current Caret position

    Inserting HTML at the caret is a common need: the user picks a command in your application and a prepared snippet lands exactly where they were typing. The method for that is InsertHtml, on the editor's content service.

    The command can sit anywhere in your own UI - an ordinary button on your form, a menu item, or a custom button you add to the editor's built-in toolbar. Toolbar buttons are added in code rather than through the Visual Studio designer: Toolbar1 and its buttons are exposed as read-only accessors and are deliberately not serialized into InitializeComponent, so that the control owns its own toolbar layout. Add a custom button to the built-in toolbar shows that in full.

    The examples below assume the editor instance is named winFormHtmlEditor1 and the button is named btnInsert.

    A custom button labelled Insert signature added in code to the end of the SpiceLogic WinForms HTML Editor's built-in toolbar, sitting alongside the factory buttons in a running application.
    A custom button labelled Insert signature added in code to the end of the SpiceLogic WinForms HTML Editor's built-in toolbar, sitting alongside the factory buttons in a running application.

    Wire that button's Click event to a handler - btnInsert_Click here - the same way you would wire any other button in your application. Your handler runs on the click, so anything it does to the editor happens against the live document:

    The custom toolbar button's Click handler after a real click in the SpiceLogic WinForms HTML Editor: the host application's activity line reports the handler ran and the signature it inserted appears at the caret.
    The custom toolbar button's Click handler after a real click in the SpiceLogic WinForms HTML Editor: the host application's activity line reports the handler ran and the signature it inserted appears at the caret.

    Now, within that click event handler, you can write code like this:

    private void btnInsert_Click(object sender, EventArgs e)
    {
        winFormHtmlEditor1.Content.InsertHtml("<span style='color:red;'>Sep 30</span> <span style='color:blue;'>renewal</span>", keepSelected: true);
    }
    Private Sub btnInsert_Click(sender As Object, e As EventArgs)
        winFormHtmlEditor1.Content.InsertHtml("<span style='color:red;'>Sep 30</span> <span style='color:blue;'>renewal</span>", keepSelected:=True)
    End Sub

    Now, you can run the application. Say your document contains a client notice, and your caret is positioned immediately before the word 'deadline'.

    The SpiceLogic WinForms HTML Editor at runtime inside a client notice application, with the text caret positioned mid-sentence immediately before the word deadline, ready for the custom button to insert HTML at that exact spot.
    The SpiceLogic WinForms HTML Editor at runtime inside a client notice application, with the text caret positioned mid-sentence immediately before the word deadline, ready for the custom button to insert HTML at that exact spot.

    Clicking the button runs that handler, and the snippet lands at the caret:

    After InsertHtml is called with keepSelected set to true, the SpiceLogic WinForms HTML Editor shows the newly inserted Sep 30 renewal text still highlighted as a live selection.
    After InsertHtml is called with keepSelected set to true, the SpiceLogic WinForms HTML Editor shows the newly inserted Sep 30 renewal text still highlighted as a live selection.

    You see that the inserted snippet is highlighted. That is because we passed the second argument of the method "keepSelected = true". If you pass that argument as false, then the inserted snippet will not be highlighted upon insertion. Anyway, once you deselect that part, you will see the snippet as follows:

    After InsertHtml is called with keepSelected set to false, the SpiceLogic WinForms HTML Editor shows the same Sep 30 renewal text inserted at the caret with no selection highlight left behind.
    After InsertHtml is called with keepSelected set to false, the SpiceLogic WinForms HTML Editor shows the same Sep 30 renewal text inserted at the caret with no selection highlight left behind.

    Ask your AI to do this

    Let your assistant do this for you. With the SpiceLogic MCP server connected, paste this into Claude Code, Cursor, or VS Code Copilot in agent mode.

    Using the SpiceLogic WinForms HTML Editor already referenced in my project, add a toolbar button labeled "Insert signature" whose click handler calls Content.InsertHtml to drop a small colored HTML snippet at the current caret position, and show me the practical difference between passing keepSelected as true versus false so I can pick the right behavior for my composer. Confirm the real InsertHtml signature through the SpiceLogic MCP tools before writing the click handler.

    Last updated on May 14, 2026

    Put this into practice.

    .NET WinForms HTML Editor Control ships with free C# and VB.NET sample projects and a 14-day evaluation.

    Prefer a guided look? Book a free live demo with our engineers - live on Zoom or Teams, never a chatbot.