Class TextBox
- Namespace
- Myra.Graphics2D.UI
- Assembly
- Myra.dll
A text input widget that allows users to enter and edit text with support for undo/redo and selection. Features include multi-line text, text wrapping, password masking, keyboard navigation, selection, clipboard operations, and full undo/redo support.
public class TextBox : Widget, IItemWithId, INotifyAttachedPropertyChanged
- Inheritance
-
TextBox
- Implements
- Inherited Members
Constructors
TextBox(Stylesheet, string)
Initializes a new instance of the TextBox class with the specified stylesheet and style.
public TextBox(Stylesheet stylesheet, string styleName = "")
Parameters
stylesheetStylesheetThe stylesheet to use for applying the style.
styleNamestringThe name of the style to apply. Defaults to the default stylesheet style.
TextBox(string)
Initializes a new instance of the TextBox class with the specified style.
public TextBox(string styleName = "")
Parameters
styleNamestringThe name of the style to apply. Defaults to the default stylesheet style.
Properties
BlinkIntervalInMs
Gets or sets the cursor blink interval in milliseconds.
public int BlinkIntervalInMs { get; set; }
Property Value
ClipToBounds
Gets or sets a value indicating whether the text box clips its content to its bounds.
public override bool ClipToBounds { get; set; }
Property Value
Cursor
Gets or sets the image displayed as the text cursor.
public IImage Cursor { get; set; }
Property Value
CursorCoords
Gets the position of the text cursor in local coordinates.
[Browsable(false)]
public Point CursorCoords { get; }
Property Value
- Point
CursorPosition
Gets or sets the zero-based index of the text cursor position.
[Browsable(false)]
public int CursorPosition { get; set; }
Property Value
Desktop
Gets or sets the desktop this text box is attached to.
public override Desktop Desktop { get; }
Property Value
DisabledTextColor
Gets or sets the color of the text when the text box is disabled.
public Color? DisabledTextColor { get; set; }
Property Value
- Color?
FocusedTextColor
Gets or sets the color of the text when the text box has focus.
public Color? FocusedTextColor { get; set; }
Property Value
- Color?
Font
Gets or sets the font used to render the text.
public SpriteFontBase Font { get; set; }
Property Value
- SpriteFontBase
HintText
Gets or sets the hint text displayed when the text box is empty.
public string HintText { get; set; }
Property Value
HintTextEnabled
Gets a value indicating whether hint text is currently displayed.
[Browsable(false)]
public bool HintTextEnabled { get; set; }
Property Value
HorizontalAlignment
Gets or sets the horizontal alignment of the text box.
public override HorizontalAlignment HorizontalAlignment { get; set; }
Property Value
MouseCursor
Gets or sets the mouse cursor type to display when hovering over the text box.
public override MouseCursorType? MouseCursor { get; set; }
Property Value
Multiline
Gets or sets a value indicating whether the text box supports multiple lines of text.
public bool Multiline { get; set; }
Property Value
OverTextColor
Gets or sets the color of the text when the mouse is over the text box, or null to use the default.
public Color? OverTextColor { get; set; }
Property Value
- Color?
PasswordField
Gets or sets a value indicating whether the text box masks input as a password field.
public bool PasswordField { get; set; }
Property Value
PressedTextColor
Gets or sets the color of the text when the text box is pressed, or null to use the default.
public Color? PressedTextColor { get; set; }
Property Value
- Color?
Readonly
Gets or sets a value indicating whether the text box is read-only.
public bool Readonly { get; set; }
Property Value
SelectEnd
Gets the zero-based index where text selection ends.
[Browsable(false)]
public int SelectEnd { get; }
Property Value
SelectStart
Gets the zero-based index where text selection starts.
[Browsable(false)]
public int SelectStart { get; }
Property Value
Selection
Gets or sets the brush used to draw the selection highlight.
public IBrush Selection { get; set; }
Property Value
Text
Gets or sets the text contained in the text box.
public string Text { get; set; }
Property Value
TextColor
Gets or sets the color of the text in the text box's normal state.
public Color TextColor { get; set; }
Property Value
- Color
TextVerticalAlignment
Gets or sets the vertical alignment of the text within the text box.
public VerticalAlignment TextVerticalAlignment { get; set; }
Property Value
VerticalSpacing
Gets or sets the vertical spacing in pixels between lines of text.
public int VerticalSpacing { get; set; }
Property Value
Wrap
Gets or sets a value indicating whether text wraps to multiple lines.
public bool Wrap { get; set; }
Property Value
Methods
ApplyStyle(WidgetStyle)
Applies the specified widget style to this text box.
protected override void ApplyStyle(WidgetStyle style)
Parameters
styleWidgetStyleThe widget style to apply.
ApplyTextBoxStyle(TextBoxStyle)
Applies the specified text box style to this text box.
public void ApplyTextBoxStyle(TextBoxStyle style)
Parameters
styleTextBoxStyleThe text box style to apply.
CopyFrom(Widget)
Copies all properties from another widget to this text box.
protected override void CopyFrom(Widget w)
Parameters
wWidgetThe widget to copy properties from.
GetWidth(int)
Gets the width of the character at the specified text index.
public float GetWidth(int index)
Parameters
indexintThe index of the character in the text.
Returns
- float
The width of the character, or 0 if the index is out of bounds or the character is a newline.
Insert(int, string)
Inserts text at the specified position in the text box.
public void Insert(int where, string text)
Parameters
InternalArrange()
Arranges the text box's content within its bounds. Sets the layout width for text wrapping.
protected override void InternalArrange()
InternalMeasure(Point)
Measures the size required to display the text box contents. Accounts for text wrapping, cursor width, and minimum line height.
protected override Point InternalMeasure(Point availableSize)
Parameters
availableSizePointThe available size for the text box.
Returns
- Point
The measured size needed for the text box.
InternalRender(RenderContext)
Renders the text box's content, including text, cursor, and selection. Handles text color selection (normal/disabled/focused), cursor blinking, and selection highlighting.
public override void InternalRender(RenderContext context)
Parameters
contextRenderContextThe render context to draw with.
OnChar(char)
Called when a character is entered into the text box.
public override void OnChar(char c)
Parameters
ccharThe character that was entered.
OnGotKeyboardFocus()
Called when the text box receives keyboard focus. Resets cursor blink state and hides hint text if present.
public override void OnGotKeyboardFocus()
OnKeyDown(Keys)
Called when a keyboard key is pressed while the text box has focus. Handles standard text editing operations: copy/paste/cut, undo/redo, navigation, text deletion, selection, and special keys like Insert, Home, End, etc.
public override void OnKeyDown(Keys k)
Parameters
kKeysThe key that was pressed.
OnLostKeyboardFocus()
Called when the text box loses keyboard focus. Shows hint text if text is empty.
public override void OnLostKeyboardFocus()
OnTouchDoubleClick()
Called when the text box receives a double-tap touch event, selecting the word at the tap position. Double-clicking on whitespace or when shift is held does nothing.
public override void OnTouchDoubleClick()
Replace(int, int, string)
Replaces a range of text at the specified position with new text.
public void Replace(int where, int len, string text)
Parameters
whereintThe zero-based index where replacement begins.
lenintThe number of characters to replace.
textstringThe replacement text.
ReplaceAll(string)
Replaces all text in the text box with the specified text.
public void ReplaceAll(string text)
Parameters
textstringThe new text to set.
SelectAll()
Selects all text in the text box.
public void SelectAll()
Events
CursorPositionChanged
Occurs when the cursor position in the text box changes.
public event MyraEventHandler CursorPositionChanged
Event Type
TextChanged
Fires every time when the text had been changed
public event MyraEventHandler<ValueChangedEventArgs<string>> TextChanged
Event Type
TextChangedByUser
Fires every time when the text had been changed by user(doesnt fire if it had been assigned through code)
public event MyraEventHandler<ValueChangedEventArgs<string>> TextChangedByUser
Event Type
TextDeleted
Fires every time when the text had been deleted
public event MyraEventHandler<TextDeletedEventArgs> TextDeleted
Event Type
ValueChanging
Fires when the value is about to be changed Set Cancel to true if you want to cancel the change
public event MyraEventHandler<ValueChangingEventArgs<string>> ValueChanging