TextField
The TextField widget provides single-line text input with cursor support, placeholder text, and keyboard navigation.
Overview
Section titled “Overview”TextField is the primary widget for text input in Elaris.UI. It supports cursor movement, text editing, and placeholder text.
Properties
Section titled “Properties”Text (string)
Section titled “Text (string)”The text content of the field.
textField.Text = "Hello";Placeholder (string)
Section titled “Placeholder (string)”Placeholder text shown when the field is empty.
textField.Placeholder = "Enter your name...";PlaceholderColor (Color)
Section titled “PlaceholderColor (Color)”Color of the placeholder text.
textField.PlaceholderColor = Color.Gray;CursorPosition (int, read-only)
Section titled “CursorPosition (int, read-only)”The current cursor position.
int pos = textField.CursorPosition;Events
Section titled “Events”KeyPress
Section titled “KeyPress”Raised when a key is pressed. Return true to mark as handled and prevent default behavior.
textField.KeyPress += (key) =>{ if (key.Key == ConsoleKey.Enter) { // Handle Enter key return true; // Mark as handled } return false; // Let default handler process it};KeyDown
Section titled “KeyDown”Raised when a key is pressed (fires before default handling).
textField.KeyDown += (key) =>{ // Key is being pressed};Raised when a key is released (fires after default handling).
textField.KeyUp += (key) =>{ // Key was released};TextChanged
Section titled “TextChanged”Raised when the text content changes.
textField.TextChanged += (newText) =>{ Console.WriteLine($"Text changed to: {newText}");};Focused
Section titled “Focused”Raised when the field receives focus.
textField.Focused += () =>{ // Field is focused};Blurred
Section titled “Blurred”Raised when the field loses focus.
textField.Blurred += () =>{ // Field lost focus};Keyboard Navigation
Section titled “Keyboard Navigation”- Left/Right Arrow: Move cursor
- Home: Move to start
- End: Move to end
- Backspace: Delete character before cursor
- Delete: Delete character after cursor
- Enter: Move cursor to end
- Tab: Navigate to next focusable widget
Usage Example
Section titled “Usage Example”var textField = new TextField("Enter your name..."){ X = 10, Y = 5, Width = 40, Height = 1, ForegroundColor = Color.White, BackgroundColor = ColorHelper.FromRgb(30, 30, 30), PlaceholderColor = Color.Gray};
textField.KeyPress += (key) =>{ if (key.Key == ConsoleKey.Enter) { Console.WriteLine($"Entered: {textField.Text}"); textField.Text = string.Empty; return true; } return false;};Related Widgets
Section titled “Related Widgets”- AutoCompleteTextField - Text field with autocomplete
- TextView - For multi-line text display
- Base Widget - Base class documentation