From Xojo Documentation
Supported PlatformsProject Types: Desktop Platforms: macOS, Windows, Linux
- For web apps, see WebTextArea.
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.
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:
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:
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:
white = RGB(255, 255, 255)
black = RGB(0, 0, 0)
If TextArea1.SelectionTextColor = White Then
TextArea1.SelectionTextColor = Black
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:
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.
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.
|#||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.
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.
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.
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.
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 Phone number, with area code
If the user tries to enter a character that is prohibited by the mask, a TextArea.ValidationError event occurs. The character that the user attempted to enter and the character position is passed to the TextArea.ValidationError event, where you can handle the keystroke as you like.
To cancel the Mask, set it to the empty string:
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:
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 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)
Loop Until stream.EndOfFile
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.
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.
Select Case Index
TextArea1.TextAlignment = TextAlignments.Default
TextArea1.TextAlignment = TextAlignments.Left
TextArea1.TextAlignment = TextAlignments.Center
TextArea1.TextAlignment = TextAlignments.Right
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.