Inserting HTML at current Caret position

    It is a very common need for a developer to insert custom html dynamically based on user's action. The most useful method for doing so is using the InsertHtml method.

    Here is an example. Say you have a button in your application and you want that, if the user clicks that button, a rich snippet will be inserted at the caret location. Say the instance name of your editor is 'winFormHtmlEditor1' and the instance name of your button is "btnInsert.

    Custom toolbar button added to the SpiceLogic WinForms HTML Editor in the Visual Studio designer, ready to wire up a Click handler that inserts a rich HTML snippet.
    Custom toolbar button added to the SpiceLogic WinForms HTML Editor in the Visual Studio designer, ready to wire up a Click handler that inserts a rich HTML snippet.

    and the click event handler for that button is "btnInsert_Click"

    Visual Studio Properties pane Events tab showing the custom toolbar button's Click event bound to the btnInsert_Click handler that calls InsertHtml on the WinForms HTML Editor.
    Visual Studio Properties pane Events tab showing the custom toolbar button's Click event bound to the btnInsert_Click handler that calls InsertHtml on the WinForms HTML Editor.

    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;'>Hello</span> <span style='color:blue;'>World</span>", keepSelected: true);
    }
    Private Sub btnInsert_Click(sender As Object, e As EventArgs)
        winFormHtmlEditor1.Content.InsertHtml("<span style='color:red;'>Hello</span> <span style='color:blue;'>World</span>", keepSelected:=True)
    End Sub

    Now, you can run the application. Lets say you have following text in your application and your caret is at the begining of the word 'awesome'.

    SpiceLogic WinForms HTML Editor at runtime with the text caret blinking at the position where the next InsertHtml call will drop the custom HTML snippet.
    SpiceLogic WinForms HTML Editor at runtime with the text caret blinking at the position where the next InsertHtml call will drop the custom HTML snippet.

    Now, simply click that new button that you just created on the toolbar. You will see your expected snippet inserted as follows:

    Result of calling InsertHtml with keepSelected = true on the WinForms HTML Editor: the freshly inserted HTML snippet is shown selected (highlighted) at the caret location.
    Result of calling InsertHtml with keepSelected = true on the WinForms HTML Editor: the freshly inserted HTML snippet is shown selected (highlighted) at the caret location.

    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 snipped will not be highlighted upon insertion. Anyway, once you deselect that part, you will see the snippet as follows:

    Result of calling InsertHtml with keepSelected = false on the WinForms HTML Editor: the inserted HTML snippet appears at the caret location without any selection highlight.
    Result of calling InsertHtml with keepSelected = false on the WinForms HTML Editor: the inserted HTML snippet appears at the caret location without any selection highlight.

    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.