Class TextBox
Represents a control that can be used to display or edit unformatted text.
[DefaultBindingProperty("Text")]
[ControlCategory("Common")]
public class TextBox : CustomTextBox, IBaseObject, IDisposableObject, ISupportInitialize, IFocusable, ITextProperty, IComponent, IControl, IWin32Window, ICustomTextBox, IDisposable, IReadOnlyStrings, IValidatorReporter, IObjectToStringOptions, INotifyDataErrorInfo, ISimpleRichTextBox
- Inheritance
-
TextBox
- Implements
- Derived
Examples
Here is how to declare a TextBox in UIXML:
<TextBox Name="textBox" Text="Text 1.1" Margin="10" TextChanged="TextBox_TextChanged" />
And the corresponding event handler in the code-behind:
private void TextBox_TextChanged(object? sender, EventArgs e)
{
var text = textBox.Text == "" ? "\"\"" : textBox.Text;
MessageBox.Show(text, string.Empty);
}
Also, a TextBox can be created from code:
var textBox = new Alternet.UI.TextBox
{
Text = "Text 1.2",
Margin = 10,
Parent = mainPanel,
};
Remarks
With the TextBox control, the user can enter text in an application.
Examples of how a TextBox can look on different platforms:
Set Text property to specify the text displayed on the control.
A TextBox, like any other Control, can be disabled by
setting its Enabled property to false
.
TextBox has rich edit capabilities. You can enable this feature by setting its
IsRichEdit property to true
.
TextBox Text Format
The multiline text controls always store the text as a sequence of lines separated by '\n' characters, i.e. in the Unix text format even on non-Unix platforms. This allows the user code to ignore the differences between the platforms but at a price: the indices in the control such as those returned by GetInsertionPoint() or GetSelectionStart() can not be used as indices into the string returned by Text as they're going to be slightly off for platforms using "\r\n" as separator (as Windows does).
Instead, if you need to obtain a substring between the 2 indices obtained from the control with the help of the functions mentioned above, you should use GetRange(). And the indices themselves can only be passed to other methods, for example SetInsertionPoint() or SetSelection().
To summarize: never use the indices returned by (multiline) TextBox as indices into the string it contains, but only as arguments to be passed back to the other TextBox methods. This problem doesn't arise for single-line platforms however where the indices in the control do correspond to the positions in the value string.
Constructors
Properties
- AutoUrl
Gets or sets a value indicating whether urls in the input text are highlighted and underlined.
- AutoUrlModifiers
Gets or sets ModifierKeys used when clicked url is autoimatically opened in the browser when AutoUrlOpen is
true
.
- AutoUrlOpen
Gets or sets a value indicating whether urls in the input text are opened in the default browser.
- CanCopy
Gets a value indicating whether the current selection can be copied, which allows theCopy() method to succeed.
- CanCut
Gets a value indicating whether the current selection can be cut, which allows theCut() method to succeed.
- CanPaste
Gets a value indicating whether the current selection can be replaced with the contents of the Clipboard, which allows thePaste() method to succeed.
- CanRedo
Gets a value indicating whether the user can redo the previous operation in the control.
- CanUndo
Gets a value indicating whether the user can undo the previous operation in the control.
- ControlKind
Returns control identifier.
- CurrentPosition
Gets or sets current position of the caret.
- DefaultAutoUrlModifiers
Gets or sets default value of the AutoUrlModifiers property.
- DefaultAutoUrlOpen
Gets or sets default value of the AutoUrlOpen property.
- EmptyTextHint
Gets or sets a hint shown in an empty unfocused text control.
- Handler
Gets control handler.
- HasBorder
Gets or sets a value indicating whether the control has a border.
- HasSelection
Gets whether control has selected text.
- HideSelection
Gets or sets a value indicating whether the selected text in the text box control remains highlighted when the control loses focus.
- HideVertScrollbar
Gets or sets a value indicating whether vertical scrollbar is hidden in the control.
- InsertionPointLineNumber
Gets the line number (zero-based) with the cursor.
- IsEmpty
Gets whether control contains text.
- IsModified
Gets or sets whether text is modified in the control.
- IsPassword
Gets or sets a value indicating whether the input text is password and all characters are shown as *.
- IsRichEdit
Gets or sets a value indicating whether text control is in rich edit mode
- LastLineNumber
Gets the last line number (zero-based).
- MaxLength
Gets or sets the maximum number of characters the user can enter into the control.
- Multiline
Gets or sets a value indicating whether this is a multiline text box control.
- ProcessEnter
Gets or sets a value indicating whether the EnterPressed event is fired.
- ProcessTab
Gets or sets a value indicating whether the TAB key is received and processed by the control.
- ReadOnly
Gets or sets a value indicating whether text in the text box is read-only.
- ReportedPosition
Gets or sets position of the caret which was reported in the event.
- Validator
Gets or sets IValueValidator for the TextBox control.
Methods
- AppendNewLine()
Appends new line to the end of the text control.
- AppendText(string)
Appends the text to the end of the text control.
- AppendTextAndStyles(IEnumerable<object>)
Appends text and styles to the end of the text control.
- Clear()
Clears all text in the control.
- ClearTextFormatting()
Clears text formatting when TextBox is in rich edit mode.
- Copy()
Copies the current selection in the control to the Clipboard.
- CreateHandler()
Creates a handler for the control.
- CreateTextAttr()
Creates new custom text style.
- CreateValidator(ValueValidatorKind)
Creates IValueValidatorText instance with the specified kind.
- CreateValidator(Type)
Creates IValueValidatorText instance for the specified type.
- CreateValidator(TypeCode)
Creates IValueValidatorText instance with the specified TypeCode.
- Cut()
Moves the current selection in the control to the Clipboard.
- DoCommand(string, params object?[])
Executes a browser command with the specified name and parameters.
- EmptyUndoBuffer()
Clears internal undo buffer. No undo operations are available after this operation.
- GetCurrentChar()
Returns current character or an empty string.
- GetDefaultStyle()
Returns the style currently used for the new text.
- GetInsertionPoint()
Returns the insertion point, or cursor, position.
- GetLastPosition()
Returns the zero based index of the last position in the text control, which is equal to the number of characters in the control.
- GetLineLength(long)
Gets the length of the specified line, not including any trailing newline character(s).
- GetLineText(long)
Returns the contents of a given line in the text control, not including any trailing newline character(s).
- GetNumberOfLines()
Returns the number of lines in the text control buffer.
- GetRange(long, long)
Returns the string containing the text starting in the positions from and up to to in the control.
- GetSelectionEnd()
Gets the current selection end position.
- GetSelectionStart()
Gets the current selection start position.
- GetStringSelection()
Gets the text currently selected in the control.
- GetStyle(long)
Returns the style at this position in the text control.
- IdleAction()
Call this method in Idle event handler in order to update information related to the current selection and caret position.
- IsValidPosition(long)
Return true if the given position is valid, i.e. positive and less than the last position.
- MoveToBeginOfText()
Moves caret to the beginning of the text.
- MoveToEndOfText()
Moves caret to the end of the text.
- OnEnterPressed()
Raises the EnterPressed event.
- OnTextChanged(EventArgs)
Called when the TextChanged event is raised.
- OnTextMaxLength()
Raises the TextMaxLength event.
- OnTextUrl(UrlEventArgs)
Raises the TextUrl event.
- Paste()
Replaces the current selection in the control with the contents of the Clipboard.
- PositionToCoords(long)
Converts given text position to client coordinates in pixels.
- PositionToXY(long)
Converts given position to a zero-based column, line number pair.
- Redo()
Redos the last edit operation in the control.
- Remove(long, long)
Removes the text starting at the first given position up to(but not including) the character at the last position.
- Replace(long, long, string)
Replaces the text starting at the first position up to(but not including) the character at the last position with the given text.
- SelectAll()
Selects all text in the control.
- SelectNone()
Clears selection. All text becomes unselected.
- SelectionAlignCenter()
Sets text alignment in the current position to Center
- SelectionAlignJustified()
Sets text alignment in the current position to Justified
- SelectionAlignLeft()
Sets text alignment in the current position to Left
- SelectionAlignRight()
Sets text alignment in the current position to Right
- SelectionSetAlignment(TextBoxTextAttrAlignment)
Sets text alignment in the current position to
alignment
.
- SelectionSetColor(Color?, Color?)
Sets foreground and background colors of the selection. If no text is selected, style of the insertion point is changed.
- SelectionToggleBold()
Toggles Bold style of the selection. If no text is selected, style of the insertion point is changed.
- SelectionToggleFontStyle(FontStyle)
Toggles FontStyle of the selection. If no text is selected, style of the insertion point is changed.
- SelectionToggleItalic()
Toggles Italic style of the selection. If no text is selected, style of the insertion point is changed.
- SelectionToggleStrikethrough()
Toggles Strikeout style of the selection. If no text is selected, style of the insertion point is changed.
- SelectionToggleUnderline()
Toggles Underline style of the selection. If no text is selected, style of the insertion point is changed.
- SetDefaultStyle(ITextBoxTextAttr)
Changes the default style to use for the new text which is going to be added to the control using WriteText(string) or AppendText(string).
- SetErrorText(ValueValidatorKnownError)
Sets ValidatorErrorText property to
knownError
.
- SetInsertionPoint(long)
Sets the insertion point at the given position.
- SetInsertionPointEnd()
Sets the insertion point at the end of the text control.
- SetSelection(long, long)
Selects the text starting at the first position up to (but not including) the character at the last position.
- SetSelectionStyle(ITextBoxTextAttr)
Changes the style of selection (if any). If no text is selected, style of the insertion point is changed.
- SetStyle(long, long, ITextBoxTextAttr)
Changes the style of the given range.
- ShowDialogGoToLine()
Shows 'Go To Line' dialog.
- ShowPosition(long)
Makes the line containing the given position visible.
- Undo()
Undoes the last edit operation in the control.
- UseValidator<T>()
Sets DataType property to
T
and Validator to the appropriate validator provider.
- WriteText(string)
Writes the text into the text control at the current insertion position.
- XYToPosition(long, long)
Converts the given zero based column and line number to a position.
Events
- CurrentPositionChanged
Occurs when CurrentPosition property value changes.
- EnterPressed
Occurs when Enter key is pressed in the control.
- HasBorderChanged
Occurs when HasBorder property value changes.
- MultilineChanged
Occurs when Multiline property value changes.
- ReadOnlyChanged
Occurs when ReadOnly property value changes.
- TextMaxLength
Occurs when maximal text length is reached.
- TextUrl
Occurs when url clicked in the text.