TextArea

From Xojo Documentation

For web apps, see WebTextArea.

Class (inherits from TextEdit)

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

Events
Close GotFocus MouseUp
ConstructContextualMenu KeyDown MouseWheel
ContextualMenuAction KeyUp Open
DragEnter LostFocus SelChange
DragExit MouseDown TextChange
DragOver MouseEnter ValidationError
DropObject MouseExit
EnableMenuItems MouseMove
Properties
Active fa-lock-32.png Left SelectionItalic
AllowAutoDeactivate LineHeight SelectionLength
AllowFocusRing LineSpacing SelectionPlain
AllowSpellChecking LiveUpdate SelectionStart
AllowStyledText LockBottom SelectionTextColor
AllowTabStop LockLeft SelectionUnderline
AllowTabs LockRight StyledText
BackgroundColor LockTop TabIndex
Bold MaximumCharactersAllowed TextAlignment
Enabled MouseCursor TextColor
FontName MouseX fa-lock-32.png Tooltip
FontSize MouseY fa-lock-32.png Top
FontUnit MultiLine fa-lock-32.png Transparent
Format Name fa-lock-32.png TrueWindow fa-lock-32.png
Handle fa-lock-32.png PanelIndex Underline
HasBorder Parent ValidationMask
HasHorizontalScrollbar ReadOnly Value
HasVerticalScrollbar Scope fa-lock-32.png VerticalScrollPosition
Height SelectedText Visible
HideSelection SelectionAlignment Width
HorizontalScrollPosition SelectionBold Window fa-lock-32.png
Index fa-lock-32.png SelectionFontName
Italic SelectionFontSize
Methods
AcceptFileDrop Close Refresh
AcceptPictureDrop Copy SelectAll
AcceptRawDataDrop DrawInto SetFocus
AcceptTextDrop InsertionPoint StyledTextPrinter
AddText Invalidate ToggleSelectionBold
CharacterPosition LineNumber ToggleSelectionItalic
CharacterPosition Paste ToggleSelectionUnderline

Notes

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).

TextArea inherits from TextEdit, the base class for both TextArea and TextField. TextField is the single-line text control. TextEdit 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 TextArea, the TextArea'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 TextArea. 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
Beep
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 TextArea, 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 TextArea. 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 TextArea, 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 TextArea. Consider the following example:

Var 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 TextArea 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 TextArea's StyledText property. Any programmatic changes to style attributes that you make while the styled text is displayed will update immediately.

Since the TextArea 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 TextArea.

For more information, see the StyledText class.

Masks

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 TextArea.ValidationFailed event occurs. The character that the user attempted to enter and the character position is passed to the TextArea.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 TextArea, you may notice some flicker as the TextArea is redrawn to show the new text. This will happen if you appended the Value property of the TextArea like this:

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

This occurs because the entire contents of the TextArea has to be redrawn. To avoid this flicker, call the AddText method instead. Simply pass it the text to be appended. For example, this code reads an external text file into a TextArea 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)
Do
TextArea1.AddText(stream.Read(1000, Encodings.UTF8))
Loop Until stream.EndOfFile
stream.Close
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 Value or SelectedText properties, this text is returned in UTF-8.

Using Class Constants

The following class constants are available to set paragraph alignments. Set the alignment of the entire contents of the TextArea by assigning a constant to the Alignment property.

Value Class Constant
0 AlignDefault
1 AlignLeft
2 AlignCenter
3 AlignRight

For example, the following code in the Pressed event of a control array sets the alignment of the text in a TextArea. The Pressed event is passed an index parameter that indicates which control was clicked.

Sub Pressed (Index As Integer)
Select Case Index
Case 0
TextArea1.TextAlignment = TextAlignments.Default
Case 1
TextArea1.TextAlignment = TextAlignments.Left
Case 2
TextArea1.TextAlignment = TextAlignments.Center
Case 3
TextArea1.TextAlignment = TextAlignments.Right
End Select

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, TextEdit, TextField classes.