# CanvasTextField class

Namespace: [Grasshopper2.UI.Canvas](https://developer.rhino3d.com/api/gh2/namespace/grasshopper2.ui.canvas.md) · Assembly: [Grasshopper2](https://developer.rhino3d.com/api/gh2/assembly/grasshopper2.md)

```cs
public sealed class CanvasTextField
```

A canvas-native, single-line editable text field.

## Constructors

- [CanvasTextField](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/ctor.md): (2 overloads)

## Properties

- [Alignment](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/alignment.md): Horizontal alignment of the text within [CanvasTextField.Bounds](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/bounds.md). Only relevant while the text fits; overflowing text always scrolls to keep the caret in view.
- [Background](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/background.md): Background colour behind the field, used only to render the digit-scrub balloon icon. Set by the host each draw so it tracks the theme. Deliberately not initialised from `SystemColors`: fields are constructed inside object constructors, which run on thread-pool threads during plugin loading, and on WPF the system colours are read off theme brushes owned by whichever thread touched them first. Reading one here from the wrong thread throws, and takes the owning object's constructor down with it.
- [Bounds](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/bounds.md): Anchor rectangle in canvas content coordinates.
- [CaretAtEnd](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/caretatend.md): Gets whether the caret sits behind the last character with no selection, i.e. a plain rightwards step has nowhere to go inside this field.
- [CaretAtStart](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/caretatstart.md): Gets whether the caret sits in front of the first character with no selection, i.e. a plain leftwards step has nowhere to go inside this field.
- [CaretDeparting](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/caretdeparting.md): Invoked when a key press wants to leave this field: an arrow at a boundary the caret cannot cross, a backspace in front of the first character, or a vertical step, which a single-line field can never absorb. Return true once the caret has been moved to a neighbouring context, which marks the key as handled; returning false (or leaving this unset) keeps the caret put.
- [CaretX](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/caretx.md): Gets the content-space x coordinate of the caret. Only meaningful after the field has been drawn at least once (its character ranges are laid out during Draw).
- [EnterPressed](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/enterpressed.md): Invoked when the user commits with Enter.
- [ErrorMessage](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/errormessage.md): Gets or sets the error message associated with the current state of the text.
- [EscapePressed](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/escapepressed.md): Invoked when the user cancels with Escape.
- [Font](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/font.md): Font used to typeset and hit-test the text.
- [GotFocus](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/gotfocus.md): Invoked when the field gains focus, before any edit is made.
- [HasFocus](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/hasfocus.md): Gets whether this field currently owns the edit focus.
- [HasScrubTarget](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/hasscrubtarget.md): Gets whether a numeric block is currently under the cursor, as of the last [CanvasTextField.MouseOver(ResponseMouseArgs)](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/mouseover.md), and a scrub would therefore have something to drag. Hosts which only forward the press conditionally need this to decide.
- [Inset](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/inset.md): Horizontal margin kept clear inside [CanvasTextField.Bounds](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/bounds.md) when typesetting. The text is laid out within the inset rectangle, so a caret at either end of the content lands just beside the boundary rather than on top of it. Hit testing and clipping still use the full bounds, so the margin remains part of the field.
- [Invalid](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/invalid.md): Gets whether the current state of the text is considered invalid. This property returns true when the [CanvasTextField.ErrorMessage](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/errormessage.md) has been set.
- [Placeholder](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/placeholder.md): Faint text shown when the field is empty and unfocused.
- [Text](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/text.md): Gets the current text content.
- [Tinting](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/tinting.md): Optional per-grapheme colour override, for syntax hinting. Given the grapheme index within the text and the grapheme cluster itself, it returns the colour to draw it in. When null, every grapheme uses the colour passed to [CanvasTextField.Draw(Context, Color, string, string)](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/draw.md). Assign this directly for something bespoke or trivial. For anything which needs to actually parse the text, build a [CanvasTextField.CharTints](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield.chartints.md) instead: it resolves the whole string once and reduces this callback to an array lookup.

## Methods

- [ClearYanker](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/clearyanker.md): Clears the scrub target (call when the mouse leaves). Returns true if it changed.
- [Defocus](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/defocus.md)
- [DeleteBackwards](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/deletebackwards.md): Delete the grapheme cluster in front of the caret, as a backspace would. Used by hosts which accept an eating departure from a neighbouring field.
- [Draw](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/draw.md): Draw the text, caret and selection.
- [Focus](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/focus.md)
- [FocusAtEnd](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/focusatend.md): Focuses the field and places the caret behind the last character, clearing any selection. Used when hopping into a field from its right.
- [FocusAtStart](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/focusatstart.md): Focuses the field and places the caret in front of the first character, clearing any selection. Used when hopping into a field from its left.
- [FocusAtX](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/focusatx.md): Focuses the field and places the caret nearest a content-space x coordinate, clearing any selection. Used to preserve the horizontal position when hopping between vertically-stacked fields.
- [KeyDown](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/keydown.md)
- [MouseDoubleClick](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/mousedoubleclick.md)
- [MouseDown](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/mousedown.md)
- [MouseDrag](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/mousedrag.md)
- [MouseOver](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/mouseover.md): Re-evaluate the digit-scrub target under a content-space point (call on hover while the scrub modifier is down). Returns true if the balloon state changed.
- [MouseUp](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/mouseup.md)
- [SetText](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/settext.md): Replaces the content without raising [CanvasTextField.TextChanged](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/textchanged.md). Ignored while focused.
- [TextInput](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/textinput.md)
- [TintFromExpression](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/tintfromexpression.md): (static) Create a [CanvasTextField.CharTints](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield.chartints.md) for fields containing expression code, as understood by [Expression](https://developer.rhino3d.com/api/gh2/type/grasshopper2.expressions.expression.md). Unlike [CanvasTextField.TintFromKind(Func<char, CanvasTextField.Syntax>)](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/tintfromkind.md) this scans the text left to right, so it can recognise what a character on its own can never reveal: decimal points, exponents, signed literals and quoted strings.
- [TintFromKind](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/tintfromkind.md): (static) Create a [CanvasTextField.CharTints](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield.chartints.md) which colours single characters by the role `classify` assigns them. Multi-character grapheme clusters and [CanvasTextField.Syntax.Plain](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield.syntax.md) characters keep the field's own colour.
- [TintFromMappingSyntax](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/tintfrommappingsyntax.md): (static) Create a [CanvasTextField.CharTints](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield.chartints.md) for fields containing path mapping notation, as understood by [PathMap](https://developer.rhino3d.com/api/gh2/type/grasshopper2.data.pathmap.md). The path portion (inside curly braces) and the item portion (inside square brackets) each use their own palette, and within each portion wildcards, integer constants, and variables including their operators are told apart.
- [TintFromTreeSyntax](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/tintfromtreesyntax.md): (static) Create a [CanvasTextField.CharTints](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield.chartints.md) for fields containing data-tree selection notation, as understood by [SiteRule](https://developer.rhino3d.com/api/gh2/type/grasshopper2.data.rules.siterule.md).

## Events

- [TextChanged](https://developer.rhino3d.com/api/gh2/type/grasshopper2.ui.canvas.canvastextfield/textchanged.md): Raised whenever the text string changes.
