TextBox and TextInput
Ask for a name and respond while the person types. The form below starts with “Gigi”; editing updates the greeting, and clearing it shows a useful empty state. A mouse click positions the caret. In the browser demo, use Home followed by Shift+End to select the current value before replacing it.
using Xui.Core.Canvas;
using Xui.Core.UI;
using Xui.Core.UI.Input;
public sealed class NameForm : VerticalStack
{
private readonly TextBox name;
private readonly Label greeting;
private readonly Label count;
public NameForm()
{
Margin = 24;
Add(new Label { Text = "What should we call you?", FontSize = 22, Margin = (0, 0, 0, 20) });
name = new TextBox
{
Text = "Gigi",
FontSize = 20,
MinimumHeight = 52,
MaximumHeight = 52,
InputOptions = new(ContentType: TextInputContentType.Name, ReturnAction: TextInputAction.Done),
Margin = (0, 0, 0, 24)
};
greeting = new Label
{
FontSize = 22,
TextColor = new Color(0x8538CEFF),
Margin = (0, 0, 0, 12)
};
count = new Label
{
FontSize = 14,
TextColor = new Color(0x655572FF)
};
Add(name);
Add(greeting);
Add(count);
UpdateGreeting();
}
protected override void OnActivate()
{
base.OnActivate();
name.Editor.Changed += OnEdited;
UpdateGreeting();
}
protected override void OnDeactivate()
{
name.Editor.Changed -= OnEdited;
base.OnDeactivate();
}
private void OnEdited(TextEditingChange change)
{
if ((change & TextEditingChange.Text) != 0)
UpdateGreeting();
}
private void UpdateGreeting()
{
greeting.Text = string.IsNullOrWhiteSpace(name.Text) ? "A name makes this yours." : $"Hello, {name.Text}!";
count.Text = $"{name.Text.Length} UTF-16 code units";
}
}
Choose the field
Xui.Core.UI.TextBox exposes direct styling properties such as FontSize, Color, SelectedColor, and SelectionBackgroundColor. It is useful when you want a small field without a design-system dependency.
Xui.ToolKit.UI.Widgets.TextInput uses the ToolKit design system for its visual properties. Both use TextInputLayer, implement ITextEditingClient, and expose their TextEditingState through Editor. Both are single-line editors; neither becomes a multiline document editor by increasing its height.
Use MinimumHeight and MaximumHeight for a fixed-height field. There is no TextBox.Height property. The example's surrounding margin keeps it away from the edge of a small viewport.
Observe a committed state change
Listen to Editor.Changed and check TextEditingChange.Text when only the content matters. Selection changes also produce notifications; they should not unnecessarily rerun validation or reload data.
The example subscribes on activation and removes its handler on deactivation. Assigning Label.Text handles invalidation for the greeting. Avoid recreating the editor after each keystroke: doing so loses focus, selection, and composition state.
Text.Length counts UTF-16 code units, as the demo explicitly labels it. It is not a count of visible characters. See Editing and selection before imposing user-visible length limits.
Hints are not validation
InputOptions describes keyboard, content, and return-action hints. The form requests name semantics and a Done action. Runtime support varies; a hint does not guarantee a particular keyboard layout or prevent pasted input.
For a numeric field, InputFilter can reject unwanted committed characters. It is a Func<char, bool>, so take care with Unicode and multi-character rules. Validate the complete value separately when accepting a form: a character filter cannot prove that a number lies within a range or that a name meets your domain rules. Programmatic Text assignment also should not be treated as validated user input.
Password masking changes presentation through IsPassword; it is not encryption or secure storage. Read IME and soft keyboards for what the host does with these fields and how composition differs from final text.