---
title: "Edit text in a rendered HTML form in C#"
url: https://recursiveai.org/onerender/cookbook/edit-text-in-rendered-form/
description: "Edit an HTML input without a browser; the host supplies input and painting."
version: "1.4.0"
---

# Edit text in a rendered HTML form in C#

**OneRender** is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneRender.Image) · [GitHub](https://github.com/recursive-ai-oss/onerender-csharp) · [Docs](https://recursiveai.org/onerender/docs/) · [Download example (.zip)](https://recursiveai.org/onerender/cookbook/edit-text-in-rendered-form/edit-text-in-rendered-form.zip)

Edit an HTML input without a browser; the host supplies input and painting.

```shell
dotnet add package RecursiveAI.OneRender.Image
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneRender.Image;
using OneRender;
using OneRender.Interaction;

var ui = InteractivePage.Create(
    "<input id=name value=Ada>",
    new HtmlPageOptions(FontProviders.CreateDefault()));
var field = ui.Page.Document.GetElementById("name");
ui.Focus(field);
ui.SetSelection(field, 3, 3);
ui.InsertText(" Lovelace");
Console.WriteLine(ui.GetValue(field));
```

```text
Ada Lovelace
```

Unzip [the example](https://recursiveai.org/onerender/cookbook/edit-text-in-rendered-form/edit-text-in-rendered-form.zip), then `dotnet run`.

## Full program

```csharp
using OneRender.Image;
using OneRender;
using OneRender.Interaction;

var ui = InteractivePage.Create(
    "<input id=name value=Ada>",
    new HtmlPageOptions(FontProviders.CreateDefault()));
var field = ui.Page.Document.GetElementById("name");
ui.Focus(field);
ui.SetSelection(field, 3, 3);
ui.InsertText(" Lovelace");
Console.WriteLine(ui.GetValue(field));
ui.HandleKey(new KeyInput { Key = "ArrowLeft", Down = true });
ui.HandleKey(new KeyInput { Key = "ArrowLeft", Down = true,
    Modifiers = KeyModifiers.Shift });
Console.WriteLine($"Selected: {ui.GetSelectedText()}");
var painter = new OneRender.Painting.Painter();
var list = new OneRender.Painting.DisplayList();
painter.Paint(ui.Page.Fragments, list);
Console.WriteLine($"Selection frame items: {list.Count}");
ui.HandleKey(new KeyInput { Key = "Backspace", Down = true });
Console.WriteLine($"After deletion: {ui.GetValue(field)}");
Console.WriteLine($"Caret available: {ui.TryGetCaretRect(out _)}");
```

```text
Ada Lovelace
Selected: c
Selection frame items: 7
After deletion: Ada Lovelae
Caret available: True
```

The full program moves left, selects one character with Shift+Left, paints the selection frame and deletes the selection. Read-only controls allow selection and copy but not edits; disabled controls do not accept editing.

## Variations

**Commit IME text.** After the snippet at the top, select the value and supply preedit, then committed text:

```csharp
ui.SelectAll();
ui.CompositionStart();
ui.CompositionUpdate("Grace");
Console.WriteLine($"During preedit: {ui.GetValue(field)}");
ui.CompositionEnd("Grace");
Console.WriteLine($"Committed: {ui.GetValue(field)}");
```

```text
During preedit: Ada Lovelace
Committed: Grace
```

## Related recipes

[Hit testing and events](https://recursiveai.org/onerender/cookbook/hit-testing-events/) · [Dark-mode form controls](https://recursiveai.org/onerender/cookbook/form-controls-dark-mode/)

---

## The short answer

Create an `InteractivePage`, find the input node, focus it, set the selection and call `InsertText`. Read the current editing value through `GetValue`, not by assuming the HTML `value` attribute is a live editing buffer. Feed movement and deletion through `HandleKey` when you want the editor’s keyboard behavior.

OneRender owns the editing session and the rendered control. The host supplies keyboard, pointer, clipboard and composition input. No browser or page script is required for this example. See the [text-editing contract](https://recursiveai.org/onerender/docs/forms/#text-editing) for the supported controls and event sequence.

## How do I edit text with HTML?

For a headless rendered UI, first decide whether you want to replace a value or perform an edit at a caret. Setting a value and inserting text are different operations. `InsertText` uses the active control’s current selection: it inserts at a collapsed caret or replaces the selected range.

The snippet focuses the field by script, moves the caret to the end of “Ada” and inserts a surname. Script focus alone puts the caret at the beginning, so omitting the explicit selection would change where the new text goes. The full program then moves left, extends the selection left with Shift and deletes the selected character.

Text-like inputs and textareas use the editing engine. Newlines are normalized, and single-line fields discard them. A number field filters its text to the documented number characters. These are control-specific behaviors, not a general rich-text document editor. The page does not provide file, color or date picker dialogs.

## How to use selection in HTML?

Use `SetSelection` for an explicit range, or feed keys and pointer input for interactive selection. The full program prints the selected substring before deletion, then paints the fragment tree into a display list while the selection is present. The list is what a rendering backend draws.

Caret, selection and composition underlines are ordinary `FillRect` paint items with stable IDs, inside the field’s clip and scrolling transform. There is no separate operating-system text box hiding behind the rendered page. `TryGetCaretRect` gives the host page-coordinate geometry when it needs to position another UI element.

Arrow movement is by grapheme cluster, and Shift extends selection. In mixed-direction text, caret movement and selection follow logical order, not visual order. Account for that limit before choosing this editor for a bidirectional-text interface. The displayed list count in the example describes that small selection frame; it is not a fixed count for every field.

## How to make input text not editable in HTML?

Use `readonly` for a field that still allows selection and copy, or `disabled` for a control that should not accept interaction. These states are not interchangeable. The editor respects them when applying changes, rather than expecting the host to filter every key itself.

For an input method editor, the host sends `CompositionStart`, one or more `CompositionUpdate` calls, then `CompositionEnd` with committed text. Preedit text is drawn but does not replace the committed value yet. The variation prints the unchanged value during preedit and the replacement after commit. An empty commit cancels composition.

Keys during composition are ignored by the editor. Supply composition events from the host’s input system rather than treating every intermediate candidate as a permanent insertion. The host also supplies time for caret blinking and an `ITextClipboard` for clipboard operations; the library does not reach into the system clipboard on its own.
