From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

For web apps, see WebTextArea.

Class (inherits from DesktopTextControl)

A DesktopTextArea control can contain multiple lines of text and can display mixed fonts, font sizes, and styles. For single-line text fields, use the DesktopTextField control.

Closing FocusReceived MouseUp
ConstructContextualMenu KeyDown MouseWheel
ContextualMenuItemSelected KeyUp Opening
DragEnter MenuBarSelected SelectionChanged
DragExit MouseDown TextChanged
DragOver MouseEnter ValidationFailed
DropObject MouseExit
FocusLost MouseMove
Active fa-lock-32.png Italic SelectionLength
AllowAutoDeactivate Left SelectionPlain
AllowFocusRing LineHeight SelectionStart
AllowSpellChecking LineSpacing SelectionTextColor
AllowStyledText LockBottom SelectionUnderline
AllowTabStop LockLeft StyledText
AllowTabs LockRight TabIndex
BackgroundColor LockTop Text
Bold MaximumCharactersAllowed TextAlignment
Enabled MouseCursor TextColor
FontName MultiLine Tooltip
FontSize Name fa-lock-32.png Top
FontUnit PanelIndex Transparent
Format Parent Underline
Handle fa-lock-32.png ReadOnly UnicodeMode
HasBorder Scope fa-lock-32.png ValidationMask
HasHorizontalScrollbar SelectedText VerticalScrollPosition
HasVerticalScrollbar SelectionAlignment Visible
Height SelectionBold Width
HideSelection SelectionFontName Window fa-lock-32.png
HorizontalScrollPosition SelectionFontSize
Index fa-lock-32.png SelectionItalic
AcceptFileDrop Copy SetFocus
AcceptPictureDrop DrawInto StyledTextPrinter
AcceptRawDataDrop InsertionPosition ToggleSelectionBold
AcceptTextDrop LineNumber ToggleSelectionItalic
AddText Paste ToggleSelectionUnderline
CharacterPosition Refresh
Close SelectAll


Styled text can be printed using the StyledTextPrinter class. Styled text can also be managed independently of a TextArea using the StyledText class (see below for more information).

DesktopTextArea inherits from DesktopTextControl, the base class for both DesktopTextArea and DesktopTextField. DesktopTextField is the single-line text control. DesktopTextControl is an abstract class and it is not intended to be instantiated.

Working With Styled Text in TextAreas

In order to display styled text in a DesktopTextArea, the DesktopTextArea's AllowStyledText property must be True.

The SelectionBold, SelectionTextColor, SelectionItalic, and SelectionUnderline properties can be used to both set the style of the selected text and get the style of the selected text. These properties are set automatically at runtime when text is selected in a DesktopTextArea. To set the style, assign True to the property. For example, if you want to add the bold style to the selected text in TextArea1, use this syntax:

TextArea1.SelectionBold = True

You can determine if the selected text is in a particular style using the same property. The following code plays a beep sound if the selected text is bold:

If TextArea1.SelectionBold Then
End If

The SelectionBold, SelectionItalic, and SelectionUnderline properties will be True if all of the selected characters are in the style being checked. Otherwise, these properties will be False.

Getting and setting the selected text font and font size work the same way using the SelectionFontName and SelectionFontSize properties. To set the font of the selected text, set the SelectionFontName property to the name of the font. If the selected text uses more than one font, SelTextFont will be an empty string. If the selected text has more than one font size, the SelectionFontSize property will be zero.

If the selected text has more than one style, font, or size, you can select individual characters using the SelStart and SelLength properties to determine which styles, fonts, and sizes are in use. Please note that if a TextArea contains more than one style, font, or font size and you reassign the value of the Text property, you will lose the styled formatting of the text substrings. All the text will then be styled uniformly. To update the text in a styled TextArea, it is safest to use the SelectionStart, SelectionLength, and SelectedText properties to update text selections.

You can also get and set the color of the selected text using the SelectionTextColor property. for example, the following code checks to see if the selected text is white and if it is, sets it to black:

Var white, black As Color
white = RGB(255, 255, 255)
black = RGB(0, 0, 0)
If TextArea1.SelectionTextColor = White Then
TextArea1.SelectionTextColor = Black
End If

To print styled text in a DesktopTextArea, use the StyledTextPrinter method to create a StyledTextPrinter object and then use the StyledTextPrinter's DrawBlock method. See the description of StyledTextPrinter for an example.

Execution order of MenuHandlers

The intrinsic control menu handlers (such as TextArea.SelectAll) are handled after any user-defined menu handlers on the TextArea subclass (if it was subclassed). This means that if you have a SelectAll handler on the Window of the TextArea, it will no longer be called when the TextArea has focus, because the TextArea will now handle it first. In this situation, create a TextArea subclass that defines its own SelectAll handler, and handle the desired behavior there.

Text Selection and Copying

The TextArea gets text selection and copy features by default without you having to do anything.

Using the StyledText Class

With the StyledText class, you can manage styled text independently of a DesktopTextArea. The styled text is contained in the Text property of the StyledText object and its style attributes can be set via its Bold, Italic, Underline, Font, Size, and TextColor methods.

When you create a StyledText object from the contents of a DesktopTextArea, the object that is returned is an alias, not a copy of the text. Therefore, subsequent changes to the StyledText object affect the contents of the DesktopTextArea. Consider the following example:

Var t As StyledText
t = TextArea1.StyledText
t.Text = "This is the new TextArea text."
t.Bold(0, 4) = True

The assignment statement changes the contents of the DesktopTextArea and the last line sets the first word in the new text to Bold.

To display the styled text, you can assign the styled text object to the DesktopTextArea's StyledText property. Any programmatic changes to style attributes that you make while the styled text is displayed will update immediately.

Since the DesktopTextArea has its own properties for getting and setting style attributes for selected text, those attributes are respected by the StyledText object while it is displayed in the DesktopTextArea.

For more information, see the StyledText class.


Use the Mask property to filter user input on a character-by-character basis and add formatting characters. For example, a mask for a Telephone number field can add parentheses, spaces, and dashes as literals, that are used for formatting, and the digit mask symbol '#' to restrict entry to numbers only.

The following table shows the characters that you can use to define a mask.

Mask Character Description
# The single digit placeholder. Entry optional. If this position is left blank in the mask, it will be rendered as a space. Plus and minus signs are allowed.

The user can type only a digit character in this position.

. Decimal placeholder.

The decimal placeholder that is actually used is specified in the user's International settings. The character is treated as a literal (formatting) character for masking purposes.

, Thousands separator.

The thousands separator that is actually used is specified in the user's International settings. The character is treated as a literal (formatting) character for masking purposes.

: Time separator.

The time separator that is actually used is specified in the user's International settings. The character is treated as a literal (formatting) character for masking purposes.

/ Date separator.

The date separator that is actually used is specified in the user's International settings. The character is treated as a literal (formatting) character for masking purposes.

\ Mask escape character.

Treat the next character in the mask as a literal. The escape character enables you to use the '#', '&', 'A', '?' (and so on) characters in the mask. The escapted character is treated as a literal (formatting) character.

& Character or space placeholder.

Valid values are the ASCII characters 32-126 and the non-ASCII characters 128-255.

C Character or space placeholder, where entry is optional. It operates like the '&' placeholder.
> Convert all the characters that follow to uppercase.

Uppercasing works beyond the ASCII range where appropriate, e.g., ü becomes Ü.

< Convert all the characters that follow to lowercase.

Lowercasing works beyond the ASCII range where appropriate, e.g., Ü becomes ü.

A Alphanumeric character placeholder, where entry is mandatory.

For example, the spec "AAA" specifies three alphanumeric characters.

a Alphanumeric character placeholder, where entry is optional.
0 Any single digit between 0 and 9. Entry is required.
9 Digit or space where entry is optional.
? Alphabetic placeholder. Entry is optional.
L Alphabetic placeholder. Entry is required.
Any literal All other symbols are displayed as literals for formatting purposes.
~ Reserved for future use. If you use "~" it will trigger an exception error. Use \~ instead.

Here are some examples:

TextArea1.ValidationMask = "###-##-####" //US Social Security number
TextArea1.ValidationMask = "(###) ###-####" //US Phone number, with area code

If the user tries to enter a character that is prohibited by the mask, a ValidationFailed event occurs. The character that the user attempted to enter and the character position is passed to the ValidationFailed event, where you can handle the keystroke as you like.

To cancel the Mask, set it to the empty string:

TextArea1.ValidationMask = ""

Adding Text to a TextArea

When adding text to a DesktopTextArea, you may notice some flicker as the DesktopTextArea is redrawn to show the new text. This will happen if you appended the Text property of the DesktopTextArea like this:

TextArea1.Text = TextArea1.Text + "my new text"

This occurs because the entire contents of the DesktopTextArea has to be redrawn. To avoid this flicker, call the AddText method instead. Pass it the text to be appended. For example, this code reads an external text file into a DesktopTextArea using the Read method of the Readable class interface. The text is read in groups of 1000 characters until the end-of-file is reached.

Var f As FolderItem
Var i As Integer
Var stream As BinaryStream
f = FolderItem.ShowOpenFileDialog(FileTypes1.Text) // file type defined in File type set
If f <> Nil Then
stream = BinaryStream.Open(f, False)
TextArea1.AddText(stream.Read(1000, Encodings.UTF8))
Loop Until stream.EndOfFile
End If

Text Encoding

TextAreas store all text internally in Unicode, which is able to represent a mixture of characters from different writing systems. When you extract the text via the Text or SelectedText properties, this text is returned in UTF-8.

Text alignment

You can set the alignment of the entire contents of the DesktopTextArea by assigning a value to the TextAlignment property.

For example, the following code in the Pressed event of a Pushbutton sets the alignment of the text in a DesktopTextArea to Center.

Sub Pressed()
TextArea1.TextAlignment = TextAlignments.Center

Drag and Drop

Dragging text between different TextAreas on a Window is supported automatically. Use the DragEnter, DragExit, DragOver and DropObject for more sophisticated handling of drag and drop.

See Also

Font, FontCount functions; Paragraph, Range, StyleRun, StyledText, StyledTextPrinter, DesktopTextControl, DesktopTextField classes.