Class
DesktopTextArea
Description
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.
Properties
Name |
Type |
Read-Only |
Shared |
---|---|---|---|
✓ |
|||
✓ |
|||
✓ |
|||
✓ |
|||
✓ |
|||
✓ |
|||
✓ |
|||
✓ |
|||
✓ |
|||
✓ |
|||
✓ |
Methods
Name |
Parameters |
Returns |
Shared |
---|---|---|---|
type As String |
|||
Type As String |
|||
text As String |
|||
LineNumber As Integer |
|||
CharPosition As Integer |
|||
Events
Name |
Parameters |
Returns |
---|---|---|
Base As DesktopMenuItem, x As Integer, y As Integer |
||
HitItem As DesktopMenuItem |
||
obj As DragItem, Action As DragItem.Types |
||
obj As DragItem, Action As DragItem.Types |
||
x As Integer, y As Integer, obj As DragItem, Action As DragItem.Types |
||
Obj As DragItem Action As DragItem.Types |
||
Key As String |
||
Key As String |
||
X As Integer, Y As Integer, DeltaX As Integer, DeltaY As Integer |
||
Enumerations
DesktopTextArea.UnicodeModes
UnicodeModes
The method by which characters will be counted for the purposes of selection or range.
Enum |
Description |
---|---|
Native |
The native mode for the platform. Codepoints is the native mode for macOS and Windows. Characters is the native mode for Linux. |
Characters |
Each logical character is counted as a single character regardless of how many bytes are actually required. |
Codepoints |
Each logical character is counted as the number of Unicode Codepoints (bytes) it requires. An emoji, for example, requires two bytes. |
Property descriptions
DesktopTextArea.Active
Active As Boolean
Indicates whether the control is active.
This property is read-only.
Active is False when the control's window is not in the foreground. When a DesktopWindow is deactivated, its controls are automatically deactivated unless AllowAutoDeactivate is set to False.
DesktopTextArea.AllowAutoDeactivate
AllowAutoDeactivate As Boolean
Determines whether the control should be deactivated (on macOS) when the parent window is deactivated. The default is True.
DesktopTextArea.AllowFocusRing
AllowFocusRing As Boolean
If True, the control indicates that it has the focus with a ring around its border; if False, the appearance of the control does not change when it has the focus.
This example is in the Initialized event of the control:
Me.AllowFocusRing = False
DesktopTextArea.AllowSpellChecking
AllowSpellChecking As Boolean
If True, words that are believed to be misspelled are underlined.
The language the user has set as their primary preferred language is language of the dictionary that is used for spell checking.
This example is in the Initialized event of a TextArea:
Me.AllowSpellChecking = True
DesktopTextArea.AllowStyledText
AllowStyledText As Boolean
If True, styled text is allowed.
The MultiLine property must also be set to True to permit styled text. If AllowStyledText is False, then the DesktopTextArea cannot have a StyledText object.
DesktopTextArea.AllowTabs
AllowTabs As Boolean
If True, pressing the Tab key will enter a tab into the control instead of moving the focus to the next item in the tab order.
This property is read-only.
DesktopTextArea.AllowTabStop
AllowTabStop As Boolean
If True, the control is in the Tab Order and accepts the focus when the user tabs into it. The default is True. If False, the user cannot tab into it to give it the focus. However, the control can gain the focus by other means, such as the user's clicking on it or by setting the focus in code.
This example removes the control from the Tab Order:
Me.AllowTabStop = False
DesktopTextArea.BackgroundColor
BackgroundColor As ColorGroup
The background color for the control.
This code sets the value of the BackgroundColor property:
Me.BackgroundColor = Color.Red
DesktopTextArea.Bold
Bold As Boolean
If True, applies the bold style to the control's text.
This property is read-only.
Mac apps can only display font styles that are available. You cannot force a font to display in bold or italic if it does not have bold or italic variations available. In this situation, the Bold property will not affect the font.
This example sets the text to Bold.
Me.Bold = True
DesktopTextArea.Enabled
Enabled As Boolean
Determines if the control should be enabled when the owning window is opened.
A disabled control cannot be clicked and cannot receive the focus.
This example disables the control.
Me.Enabled = False
DesktopTextArea.FontName
FontName As String
Name of the font used to display the text content.
You can enter any font that is installed on the computer or the names of the two metafonts, "System" and "SmallSystem".
The System font is the font used by the system software as its default font. Different operating systems use different default fonts. If the system software supports both a large and small System font, you can also specify the "SmallSystem" font as your TextFont.
On macOS, "SmallSystem" specifies the OS's smaller system font and may make the control smaller in size as well. On Windows and Linux, "SmallSystem" is the same as "System".
This code sets the FontName property.
Me.FontName = "Helvetica"
DesktopTextArea.FontSize
FontSize As Single
Point size of the font used to display text content of a DesktopControl.
If you enter zero as the FontSize, your app will use the font size that works best for the platform on which it is running.
This code sets the font size to 16 points.
Me.FontSize = 16
DesktopTextArea.FontUnit
FontUnit As FontUnits
The units in which FontSize is measured.
See the FontUnits enumeration for valid values.
This example is in the Initialized event of the control. It sets the font unit to Pixel.
Me.FontUnit = FontUnits.Pixel
DesktopTextArea.Format
Format As String
Formats the contents of the control when it loses the focus.
It uses the same formatting conventions as the Format function. To turn off formatting, set the Format property to the empty string, "".
If you need to enforce an entry format rather than permitting any entry, use the Format property in conjunction with the Mask property. See the description of the ValidationMask property and the section "Masks" in the Notes section.
This line applies a dollar format when the user tabs out of the field.
Me.Format = "\$###,###.##"
DesktopTextArea.FormattedText
FormattedText As String
Returns the Text property formatted using the value in the Format property.
This property is read-only.
DesktopTextArea.Handle
Handle As Ptr
Returns a handle to the control.
This property is read-only.
For interfacing with Mac APIs using Declares, DesktopControl.Handle returns NSViews (except for DesktopToolbar).
On Windows returns the HWND of the control.
On Linux it returns a GtkWidget.
The following gets a handle to the control.
Var p As Ptr = Me.Handle
DesktopTextArea.HasBorder
HasBorder As Boolean
Indicates whether or not the border is visible.
Note
Setting this at runtime is not currently supported for Windows.
DesktopTextArea.HasHorizontalScrollbar
HasHorizontalScrollbar As Boolean
If True, the DesktopTextArea has a horizontal scrollbar and text does not wrap at the right edge of the DesktopTextArea.
DesktopTextArea.HasVerticalScrollbar
HasVerticalScrollbar As Boolean
If True, the DesktopTextArea includes a vertical scrollbar.
DesktopTextArea.Height
Height As Integer
The height (in points) of the control.
This example sets the height of the control to 100:
Me.Height = 100
DesktopTextArea.HideSelection
HideSelection As Boolean
HideSelection hides the selection highlight when the DesktopTextArea loses the focus. The default is True.
If HideSelection is False, then the DesktopTextArea retains its selection when another field gains the focus. HideSelection is currently a Windows only property.
DesktopTextArea.HorizontalScrollPosition
HorizontalScrollPosition As Integer
Scrolls the text control horizontally to the position (in points) from the left edge of the control.
This code scrolls the control horizontally by 200 points.
TextArea1.HorizontalScrollPosition = 200
DesktopTextArea.Index
Index As Integer
If the control is used in a control set, this specifies the control's index in the set.
This property is read-only.
DesktopTextArea.Italic
Italic As Boolean
If True, applies the italic style to the control's text.
This property is read-only.
Mac apps can only display font styles that are available. You cannot force a font to display in bold or italic if it does not have bold or italic variations available. In this situation, the Italic property will not affect the font.
The following sets the Italic property for the control.
Me.Italic = True
DesktopTextArea.Left
Left As Integer
The distance from the left side of the control to the left side of its containing window or container.
The following example moves the control 100 points from the left side of the window:
Me.Left = 150
DesktopTextArea.LineHeight
LineHeight As Double
Controls the height of each line in the TextArea control (it is global so the effect spans all paragraphs). A value of 0 maintains the default line height of the control, while any other value changes the height of each line. The value is tied to the TextUnit property, so if the TextUnit is in Inches then the LineHeight value specifies a height in Inches.
Note
Due to lack of support in GTK, LineHeight does not have any effect on Linux.
Be aware that LineHeight is the exact and literal height that the line will be treated as -- text can overlap if the value is too low.
DesktopTextArea.LineSpacing
LineSpacing As Double
This controls the spacing between lines. So if you wanted your lines to be double spaced, you would enter a value of 2. The default value is 1, or single spaced.
On Linux, LineSpacing is not accurate if the TextArea has variable text sizes.
DesktopTextArea.LockBottom
LockBottom As Boolean
Determines whether the bottom edge of the control should stay at a set distance from the bottom edge of the parent control, if there is one, or the owning window.
This property can be set in the control's Inspector. The following example sets it in code.
Me.LockBottom = True
DesktopTextArea.LockLeft
LockLeft As Boolean
Determines whether the left edge of the control should stay at a set distance from the left edge of the parent control, if there is one, or the owning window.
LockLeft and Locktop default to True when you add a new control to a window. Existing controls will be altered only if LockRight and/or LockBottom are not set. LockLeft has no effect unless LockRight is True.
This property can be set in the control's Inspector. The following example sets it in code.
Me.LockLeft = True
DesktopTextArea.LockRight
LockRight As Boolean
Determines whether the right edge of the control should stay at a set distance from the right edge of the parent control, if there is one, or the owning window.
This property can be set in the control's Inspector. The following example sets it in code.
Me.LockRight = True
DesktopTextArea.LockTop
LockTop As Boolean
Determines whether the top edge of the control should stay at a set distance from the top edge of the parent control, if there is one, or the owning window.
LockTop and LockLeft default to True when you add a control to a window. Existing controls will be altered only if LockRight and/or LockBottom are not set. LockTop has no effect unless LockBottom is True.
This property can be set in the control's Inspector. The following example sets it in code.
Me.LockTop = True
DesktopTextArea.MaximumCharactersAllowed
MaximumCharactersAllowed As Integer
The maximum number of characters allowed in the DesktopTextArea.
The value of zero does not limit text. MaximumCharactersAllowed works for normal text entry, copy and paste, and drag and drop.
There may be maximum limits imposed by the operating system.
DesktopTextArea.MouseCursor
MouseCursor As MouseCursor
The cursor to be displayed while the mouse is within the control and both the DesktopApplication and DesktopWindow class's MouseCursor properties are Nil.
If the DesktopApplication class's MouseCursor property is not Nil or the DesktopWindow's MouseCursor property is not Nil, then any control's MouseCursor property is ignored. You can use a cursor stored in the Cursors module. On Macintosh, you can also obtain a MouseCursor from a resource file.
This line in the Opening event of the control sets the default cursor to the finger pointer.
Me.MouseCursor = System.Cursors.FingerPointer
DesktopTextArea.MultiLine
MultiLine As Boolean
If True, wrap text to multiple lines and allow more than 32K of text.
Changing this property at runtime has no effect.
Page Up, Page Down, Home and End keys are supported for multiline TextAreas. A vertical scroll bar is added automatically when MultiLine is True.
MultiLine TextAreas support any standard terminator (Return, Linefeed, or Return + Linefeed) to indicate the end of a paragraph.
DesktopTextArea.Name
Name As String
The name of the control. Set the name of the control in the Inspector.
This property is read-only.
DesktopTextArea.PanelIndex
PanelIndex As Integer
If the control has been placed on a DesktopTabPanel or DesktopPagePanel control, this is the panel (page/tab) that the control is on. If the control is not on a panel, it returns -1.
The first panel is numbered zero. If the control has been placed on a panel of a DesktopTabPanel or DesktopPagePanel control, it returns the panel number. If the control is not on a DesktopPagePanel or DesktopTabPanel, it returns -1. If you change the PanelIndex to a nonexistent panel, the control will disappear until you give it a PanelIndex value that corresponds to a panel that exists.
If you are looking to change the currently selected panel (page/tab), use DesktopPagePanel.SelectedPanelIndex.
This code displays the panel index of the control that is on the page.
MessageBox(Me.SelectedPanelIndex.ToString)
DesktopTextArea.Parent
Parent As Object
Used to get and set the control's parent control or window.
If the control is on the window, the Parent will be the Window. If the control is on the container, the Parent will be the container. If the control is completed enclosed by another control, the Parent will be that control.
If you do not want the enclosing control to be the parent, set the Parent property of that control to Nil to make it the Window.
If the parent control is somehow in another window, an InvalidParentException will occur.
The following example sets the parent of the control to the window.
Me.Parent = Nil
DesktopTextArea.ReadOnly
ReadOnly As Boolean
If True, the text of the control cannot be modified.
This example is in the Opening event of the control.
Me.ReadOnly = True
DesktopTextArea.Scope
Scope As Integer
Used to determine whether access to the control is Public (0) or Private (2). The default is Public.
This property is read-only.
Note
This is a designtime-only property and thus can only be set in the Inspector and is not accessible via code.
If the Scope of a control is set to Private, it cannot be accessed from outside its parent window.
DesktopTextArea.SelectedText
SelectedText As String
The currently selected text.
This example places the selected text from a DesktopTextArea into a DesktopTextField.
TextArea1.SelectionStart = 0
TextArea1.SelectonLength = 10
TextField1.Text = TextArea1.SelectedText
DesktopTextArea.SelectionAlignment
SelectionAlignment As Integer
Controls paragraph alignment of the selected text.
Use TextAlignments with this property.
This example sets the alignment of the selected paragraph in TextArea1 to centered:
TextArea1.SelectionAlignment = TextAlignments.Center
This example displays a dialog box if the selected paragraph is centered.
If TextArea1.SelectionAlignment = TextAlignments.Center then
MessageBox("The text is centered.")
End If
DesktopTextArea.SelectionBold
SelectionBold As Boolean
Used to get and set the bold style of the selected text.
DesktopTextArea.SelectionFontName
SelectionFontName As String
Used to get and set the text font of the selected text.
When used in an unstyled DesktopTextArea, it changes the font of all the text in the DesktopTextArea.
DesktopTextArea.SelectionFontSize
SelectionFontSize As Single
Used to get and set the font size of the selected text.
When used in an unstyled DesktopTextArea it changes the font size of all the text in the DesktopTextArea.
DesktopTextArea.SelectionItalic
SelectionItalic As Boolean
Used to get and set the italic style of the selected text.
DesktopTextArea.SelectionLength
SelectionLength As Integer
The number of selected characters.
A SelectionLength of 0 means an insertion point rather than a selection. A value greater than the number of characters in the control means that the selection is from SelectionStart to the end of the control.
On Windows, selecting text via code does not display the text as selected unless the control has the focus. If you want this behavior, call SetFocus before setting the selection.
This example selects the first 10 characters of TextArea1.
TextArea1.SelectionStart = 0
TextArea1.SelectionLength = 10
DesktopTextArea.SelectionPlain
SelectionPlain As Boolean
If True, the selected text is plain. If the selected text is styled (e.g., Bold), setting SelectionPlain to True changes it to Plain.
DesktopTextArea.SelectionStart
SelectionStart As Integer
The position of the insertion point.
A value of zero means that the insertion point is before the first character. Use SelectionStart to set the position of the insertion point in the text. Use SelectionStart in conjunction with SelectionLength to select a portion of the text in the control, beginning with SelectionStart and extending for SelectionLength characters.
On Windows, selecting text via code does not display the text as selected unless the control has the focus. If you want this behavior, call SetFocus before setting the selection.
This example selects the first 10 characters of TextArea1.
TextArea1.SelectionStart = 0
TextArea1.SelectionLength = 10
DesktopTextArea.SelectionTextColor
SelectionTextColor As Color
Used to get and set the text color of the selected text.
When used in an unstyled DesktopTextArea, it changes the color of all the text in the DesktopTextArea.
DesktopTextArea.SelectionUnderline
SelectionUnderline As Boolean
Used to get and set the underline style of the selected text.
DesktopTextArea.StyledText
StyledText As StyledText
Enables you to access the properties and methods of the StyledText class for the text in the DesktopTextArea.
If the Styled property is Off then this property returns Nil.
The DesktopTextArea must have its MultiLine and Styled properties set to True. The object returned by StyledText is not a copy, so subsequent changes to it will affect the contents of the DesktopTextArea. More information about this is available.
DesktopTextArea.TabIndex
TabIndex As Integer
The control's position in the Tab Order. The control with whose TabIndex is 0 will get the focus first.
On macOS, only controls where you enter data from the keyboard typically get the focus. In order to manually set focus to controls that don't allow keyboard entry, go to System Preferences, click on the Keyboard icon then on the Shortcuts tab and then check the Use keyboard navigation to move focus between controls checkbox.
This example sets the control's TabIndex.
Me.TabIndex = 2
DesktopTextArea.Text
Text As String
The text displayed.
Assigning a string to the Text property replaces the entire contents of the control. The font, size, and color are uniform and match the values last set with the FontName, FontSize, and TextColor properties, or if these haven't been used, the settings in the IDE.
Please note that the implementation of the DesktopTextField and DesktopTextArea controls is platform dependent. Do not expect that the text you assign is the same as the text you receive from this property. For example on Mac your text is converted to UTF-8 internally. On Windows all text after a null character (which means chr(0) and not "0") is ignored. End of line characters can change to their platform specific counterpart. Special characters with Asc values below 32 may be ignored or removed.
Clear (remove) any text that is displayed in the TextArea:
TextArea1.Text = ""
This example places the selected text from a DesktopTextArea into a DesktopTextField.
TextArea1.SelectionStart = 0
TextArea1.SelectionLength = 10
TextField1.Text = TextArea1.SelectedText
DesktopTextArea.TextAlignment
TextAlignment As TextAlignments
Sets the paragraph alignment for entire contents of the control.
Use the TextAlignments enumeration to get/set the value of this property.
For the DesktopTextArea control, use the SelectionAlignment property to set paragraph alignments for individual paragraphs. Currently the Default alignment is the same as Left aligned. Setting the alignment on Linux requires GTK+ 2.4 or greater.
This example is in the Initialized event of the control.
Me.TextAlignment = TextAlignments.Center
DesktopTextArea.TextColor
TextColor As ColorGroup
Gets or sets the color of the text. The default value is black.
The following example sets the TextColor.
Me.TextColor = Color.Red
DesktopTextArea.Tooltip
Tooltip As String
Text of help message displayed as a Windows or Linux "tooltip" or macOS help tag.
The tip/tag is displayed when the user hovers the mouse cursor over the control.
This example adds a tooltip to a control:
Me.Tooltip = "Click to bark."
DesktopTextArea.Top
Top As Integer
The distance from the top of the control to the top of its containing window or container.
This example sets the top of the control to 140 points from the top of the window:
Me.Top = 140
DesktopTextArea.Transparent
Transparent As Boolean
Determines whether the control is transparent on Windows. The default is False. Has no effect on macOS or Linux.
Transparent controls draw more slowly and use more memory in order to cache the background. Unless you absolutely require transparency, leave this set to False.
For projects that were created prior to 2018r1, this property defaults to True to emulate previous behavior. Consider changing it to False to improve performance if you do not require transparency.
DesktopTextArea.Underline
Underline As Boolean
If True, applies the underline style to the control's text.
This property is read-only.
This code underlines the control's text or caption property.
Me.Underline = True
DesktopTextArea.UnicodeMode
UnicodeMode As TextArea.UnicodeModes
The method by which the method SelectionLength will count the selection.
Choosing Characters will result in a count of the number of logical characters in the selection. For example, a emoji would be counted as a single character. Choosing CodePoints will result in a count of the number of Unicode CodePoints (basically bytes) that the characters in the selection consume. An emoji, for example, requires two bytes.
The default setting is Characters on all platforms. However, for backwards compatibility, DesktopTextArea controls on layouts created in projects prior to 2020r1 will be set to Native which means CodePoints on macOS and Windows and Characters on Linux.
DesktopTextArea.ValidationMask
ValidationMask As String
Use the ValidationMask 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. It uses the same mask characters as Visual Basic. If the user enters a character that is prohibited by the mask, it causes the ValidationFailed event to occur.
You can use the ValidationMask property in conjunction with the Format property. The ValidationMask filters the entry, while the Format property applies a format to the entry. The formats are the same as for the Format function.
The DesktopTextField will autocomplete any literal mask characters for the user. If the user uses the Backspace key he/she can backup the autocompletion but only 1 character at a time.
To turn off the ValidationMask, set it to the empty string, "".
You can set the ValidationMask property in the Properties pane. If you use the "#" character as the first character of the mask, there is the remote possibility that it may be confused with the name of a constant. When you use a constant in the Properties pane, you precede its name with the "#" sign. If you want both a constant and a mask to be named "Example", then you should create a new constant named "poundExample" with its value set to "#Example" and assign that in the Properties pane.
The following table shows the characters that you can use to define a mask.
Mask Character |
Description |
---|---|
# |
The single digit placeholder. The user can type only a digit (numeric) character in this position. For example, the mask "(###) ###-####" accepts the entry 5551212121" and returns "(555) 121-2121". |
. |
Decimal placeholder. |
, |
Thousands separator. |
: |
Time separator. |
/ |
Date separator. |
Mask escape character. |
|
& |
Character or space placeholder. It accepts one character. |
C |
Character or space placeholder, where entry is optional. It operates like the '&' placeholder. For example, the mask "CCCC-CC" formats "1233ed" as "1233-ed". |
> |
Convert all the characters that follow to uppercase. |
< |
Convert all the characters that follow to lowercase. |
A |
Alphanumeric character placeholder, where entry is mandatory. |
a |
Alphanumeric character placeholder, where entry is optional. |
0 |
The literal "0" (zero). For example, the mask "99.00" formats the entry "22" as "22.00". The mask "CC0-9999" accepts the entry "1234" and returns it as "CC0-1234". The mask "##,###.00" accepts the entry "12345" and returns "12,345.00". The mask "99.00" accepts "21" and returns "21.00". |
9 |
A Single (numeric) digit, where entry is optional. |
? |
Alphabetic placeholder. Entry is optional. For example, the mask "???" accepts three alphabetic characters. It rejects numeric characters. |
Any literal |
All other symbols are displayed as literals for formatting purposes. For example, the mask "99[9]" accepts the entry "333" and returns "33[3]". |
~ |
Reserved for future use. If you use "~" it will trigger an exception error. Use ~ instead. |
Mask |
Description |
---|---|
999,999.99 |
Formats 11122233 as "111,222.33" (Using the US thousands separator.) |
99.00 |
Formats "22" as "22.00." |
###-##-#### |
US Social Security number. Fomats "123456789" as 123-45-6789. |
:
TextField1.ValidationMask = "###-##-####"
TextField1.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 ValidationMask, set it to the empty string:
TextField1.ValidationMask = ""
DesktopTextArea.VerticalScrollPosition
VerticalScrollPosition As Integer
Use to get or set the topmost visible line (zero-based).
Use this property to determine the first visible line; set this property to scroll the control. Setting it to the last line or beyond the last line will move the vertical scrollbar's thumb to the bottom, not necessarily displaying the last line at the top of the control.
DesktopTextArea.Visible
Visible As Boolean
Determines whether the control is visible when its owning window is opened. The default is True: the control is visible.
The following code makes the control invisible:
Me.Visible = False
DesktopTextArea.Width
Width As Integer
The width (in points) of the control.
The following example resizes the control:
Me.Width = 200
DesktopTextArea.Window
Window As DesktopWindow
The control's parent window.
This property is read-only.
This code gets the parent window's Title property.
MessageBox(Me.Window.Title)
Method descriptions
DesktopTextArea.AcceptFileDrop
AcceptFileDrop(type As String)
Permits documents of type type to be dropped on the control. type must be a file type that you defined in via the FileType class or the File Type Sets Editor.
This code in the Opening event makes it possible for the user to drop either a picture or a file that is a jpeg image. The File Type Sets editor was used to define the “image/jpeg” file type. It is one of the “Common File Types” that is available in the editor.
Me.AcceptPictureDrop
Me.AcceptFileDrop("image/jpeg")
To restrict file drops to just folders (and not files), you can put this code in the DragEnter event:
If Not obj.FolderItem.IsFolder Then Return True
DesktopTextArea.AcceptPictureDrop
AcceptPictureDrop
Permits pictures to be dropped on the control.
If a control should accept pictures in a drag and drop, then AcceptPictureDrop needs to be called prior to the drop. Typically, it is in the Opening event of the control itself. For example, the line:
Me.AcceptPictureDrop
in the Opening event of the control that will receive the dragged pictures is needed. When the picture is dropped, the DropObject event is called and this is where you will put your code to handle the drop.
Opening Event:
Me.AcceptPictureDrop
DropObject Event:
If obj.PictureAvailable Then
Me.Backdrop = obj.Picture
End If
DesktopTextArea.AcceptRawDataDrop
AcceptRawDataDrop(Type As String)
Permits data (of the Type specified) to be dropped on the control.
The following specfies a generic file type defined in the File Type Sets editor.
Me.AcceptRawDataDrop("????")
DesktopTextArea.AcceptTextDrop
AcceptTextDrop
Permits text to be dropped on the control.
This line in the Opening event of a control that can accept dragged text.
Me.AcceptTextDrop
DesktopTextArea.AddText
AddText(text As String)
Adds the passed text to the current Text. Call AddText rather than using the + operator to add text to existing text.
This example reads a text field in blocks of 1000 characters using Read.
Var f As FolderItem
Var dlg As OpenDialog
Var t As TextInputStream
' create a new openDialog
dlg = New OpenFileDialog
' set what type of file it looks for
dlg.Filter = "text/plain"
' run the dialog
f = dlg.ShowModal
' check to make sure the user didn't click cancel
If f <> Nil Then
Try
t = TextInputStream.Open(f)
' Read all of the text, 1000 characters at a time
' into the TextField
While Not t.EndOfFile
myTextArea.AddText(t.Read(1000, Encodings.UTF8))
Wend
t.Close
Catch error As IOException
' the file could not be a read as a text file
MessageBox("The selected file is not a text file. Error: " + error.Message)
End If
Else
' the user clicked cancel... just ignore it
End If
DesktopTextArea.CharacterPosition
CharacterPosition(LineNumber As Integer) As Integer
Returns as an Integer the character position for pixel coordinates X, Y relative to the control.
Characters are numbered consecutively from the start until the end of the control. The first character is numbered 1. The first line is numbered zero.
This example gets the character position of the first character of the second line.
Var i As Integer
i = TextArea1.CharacterPosition(1)
This example is in the MouseDown event of the control. The event passes in the X,Y coordinates of the MouseDown event. The example gets the position of the character at the passed coordinates.
Var i As Integer
i = Me.CharacterPosition(x, y)
MessageBox(i.ToString)
Return True
DesktopTextArea.Close
Close
Closes a control.
Closing a control permanently removes the control from memory, making it impossible to access. You can close both non-indexed controls and indexed controls. When you close an indexed control, the indexes for the remaining controls will shift downward so that the indexes start with zero and are consecutive.
The following code closes the control. When this is executed from a visible control, the control disappears from the window.
Me.Close
DesktopTextArea.Copy
Copy
Copies the selected text in the TextArea to the Clipboard, including the styled text information.
This example copies the selected text and places it on the Clipboard.
TextArea1.SelectionStart = 0
TextArea1.SelectionLength = 10
TextArea1.Copy
DesktopTextArea.DrawInto
DrawInto(g As Graphics, x As Integer, y As Integer)
Draws the contents of the control into the specified Graphics context. The parameters x and y are the coordinates of the top, left corner.
Note
DrawInto will only work if the control is on a window or container.
This example draws the current control into the Graphics of a Picture and then displays it as the Backdrop of a Canvas:
Var p As New Picture(Me.Width, Me.Height)
Me.DrawInto(p.Graphics, 0, 0)
Canvas1.Backdrop = p
DesktopTextArea.InsertionPosition
InsertionPosition(X As Integer, Y As Integer) As Integer
Returns (as an Integer) returns the position of the insertion point closest to pixel coordinates X,Y relative to the control. The returned value is zero-based.
This example is in the MouseDown event of a DesktopTextArea. This event passes in the X,Y coordinates of the MouseDown event and the method returns the corresponding character position.
Var i As Integer
i = TextArea1.InsertionPosition(x, y)
MessageBox(i.ToString)
Return True
DesktopTextArea.LineNumber
LineNumber(CharPosition As Integer) As Integer
Returns (as an Integer) the line number in which the CharacterPosition character appears.
Characters are numbered consecutively with the first character numbered 1. The first line is numbered zero. Used with ScrollPosition, this lets you scroll the control to a particular place in the text.
The line number reflects line breaks from both hard line breaks in the text and soft line breaks caused by word-wrapping within the control.
Gets the line number for the character at position 100:
Var lineNum As Integer = TextArea1.LineNumber(100)
This example searches a TextArea using text provided in a TextField and then scrolls to the text in the TextArea:
Var searchText As String = TextField1.Text
Var position As Integer = TextArea1.Text.IndexOf(searchText)
If position > -1 Then
TextArea1.VerticalScrollPosition = TextArea1.LineNumber(position)
Else
' IndexOf returns -1 if no match was found.
End If
DesktopTextArea.Paste
Paste
Pastes the styled or unstyled text on the Clipboard into the editing area at the insertion point, adding the text to the existing text.
When pasting into a DesktopTextArea, if the text is styled, the style information is preserved.
This example pastes the contents of the Clipboard into TextArea2 at the insertion point.
TextArea2.Paste
DesktopTextArea.Refresh
Refresh(immediately As Boolean = False)
Redraws the portion specified of the contents of the control the next time the OS redraws the control or immediately if True is passed.
If you are calling this so frequently that you experience a slowdown in your code, pass True for the immediately parameter.
Calling this method causes the Render event to fire.
Refresh the entire area immediately:
Me.Refresh(True)
Refresh a portion of the area the next time the OS redraws the control:
Me.Refresh(100, 150, 200, 300)
DesktopTextArea.SelectAll
SelectAll
Selects all of the text in the control. If there is no text in the control, SelectAll does nothing.
This example selects all the text in the control.
TextArea1.SelectAll
DesktopTextArea.SetFocus
SetFocus
Gives the DesktopTextArea the focus, sending all keydown events to the DesktopTextArea and moving the cursor there.
DesktopTextArea.StyledTextPrinter
StyledTextPrinter(g As Graphics, Width As Integer) As StyledTextPrinter
Used to create a StyledTextPrinter object for printing the TextArea's text property as styled text.
The DesktopTextArea's MultiLine property must be True to support styled text printing. Returns a StyledTextPrinter object. Width (pixels) is width of printable area.
Important
This method is only supported for macOS.
DesktopTextArea.ToggleSelectionBold
ToggleSelectionBold
Reverses the bold style of the highlighted text.
DesktopTextArea.ToggleSelectionItalic
ToggleSelectionItalic
Reverses the italic style of the highlighted text.
DesktopTextArea.ToggleSelectionUnderline
ToggleSelectionUnderline
Reverses the underline style of the highlighted text.
Event descriptions
DesktopTextArea.Closing
Closing
The control is closing.
DesktopTextArea.ConstructContextualMenu
ConstructContextualMenu(Base As DesktopMenuItem, x As Integer, y As Integer) As Boolean
This event is called when it is appropriate to display a contextual menu for the control.
This event handler is the recommended way to handle contextual menus because this event figures out whether the user has requested the contextual menu, regardless of how they did it. Depending on platform, it might be in the MouseUp or MouseDown event and it might be a right+click or by pressing the contextual menu key on the keyboard, for example.
Base is analogous to the menu bar for the contextual menu. Any items you add to Base will be shown as menu items. If you return False, the event is passed up the parent hierarchy.
If you return True, the contextual menu is displayed. The parameters x and y are the mouse locations. If the event was fired because of a non-mouse event, then x and y are both set to -1. See the example of a contextual menu in the following section.
The following ConstructContextualMenu event handler builds a menu with three menu items plus a submenu with three additional menu items.
' Add some items
base.AddMenu(New DesktopMenuItem("Test 1"))
base.AddMenu(New DesktopMenuItem("Test 2"))
base.AddMenu(New DesktopMenuItem("Test 3"))
' Add a Separator
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))
' Add a sub menu
Var submenu As New DesktopMenuItem("SubMenu")
submenu.AddMenu(New DesktopMenuItem("SubMenu Test 1"))
submenu.AddMenu(New DesktopMenuItem("SubMenu Test 2"))
submenu.AddMenu(New DesktopMenuItem("SubMenu Test 3"))
base.AddMenu(submenu)
' Add a Separator
base.AddMenu(New DesktopMenuItem(DesktopMenuItem.TextSeparator))
Return True
DesktopTextArea.ContextualMenuItemSelected
ContextualMenuItemSelected(HitItem As DesktopMenuItem) As Boolean
Fires when a contextual menuitem selectedItem was selected but the MenuItemSelected event and the MenuHandler for the DesktopMenuItem did not handle the menu selection.
This event gives you a chance to handle the menu selection by inspecting the menuitem's Text or Tag properties to see which item was selected. Use this in conjunction with ConstructContextualMenu if you have not specified the MenuItemSelected event or the Menu Handler for the items on the contextual menu. See the example of a contextual menu in the examples for the DesktopUIControl class.
Return True if this event has handled the item the user chose from the contextual menu. Returning False will cause the control's parent to execute its ContextualMenuItemSelected event. This can be handy if you have the same contextual menu for several controls who share the same Parent (several on the same window for example). By returning False you can handle them all in a single event.
This simple event handler displays the value of the selected menu item.
If selectedItem <> Nil Then MessageBox(selectedItem.Text)
Return True
DesktopTextArea.DragEnter
DragEnter(obj As DragItem, Action As DragItem.Types) As Boolean
Fires when the passed DragItem enters the DesktopUIControl.
Returns a Boolean. Return True from this event to prevent the drop from occurring.
The Action parameter specifies the type of drag action.
To restrict file drops to just folders (and not files), you can put this code in the DragEnter event:
If Not obj.FolderItem.IsFolder Then Return True
DesktopTextArea.DragExit
DragExit(obj As DragItem, Action As DragItem.Types)
Fires when the passed DragItem exits the DesktopUIControl.
The Obj parameter is the item being dragged. The Action parameter specifies the type of drag action.
DesktopTextArea.DragOver
DragOver(x As Integer, y As Integer, obj As DragItem, Action As DragItem.Types) As Boolean
Fires when the DragItem is over the DesktopUIControl.
The Obj parameter is the object being dragged. The coordinates x and y are relative to the DesktopUIControl. Returns a Boolean. Return True from this event to prevent the drop from occurring.
The Action parameter specifies the type of drag action, which is typically done by holding down a modifier key (Shift, Alt, Option, Command, etc.) while doing the drag.
DesktopTextArea.DropObject
DropObject(Obj As DragItem Action As DragItem.Types)
The item represented by Obj has been dropped on the control.
The Obj parameter is the object being dragged. The Action parameter specifies the type of drag action.
The following DropObject event handler can handle either a dropped picture or a dropped file. The type of file that it can handle needs to have been specified in a call to AcceptFileDrop prior to the drop, for example, in the Opening event.
If Obj.PictureAvailable Then
Me.Image = obj.Picture
ElseIf Obj.FolderItemAvailable Then
Me.Image = Picture.Open(obj.FolderItem)
End If
DesktopTextArea.FocusLost
FocusLost
The control has lost the focus.
Note
On macOS, controls other than text fields and lists will accept and lose focus only if the full keyboard access option is enabled in System Preferences/Keyboard.
DesktopTextArea.FocusReceived
FocusReceived
The control has received the focus and has a selection rectangle around it.
Note
On macOS, controls other than text fields and lists will accept focus only if the full keyboard access option is enabled in System Preferences/Keyboard.
DesktopTextArea.KeyDown
KeyDown(Key As String) As Boolean
The user has pressed the Key passed while the control has the focus.
How KeyDown works depends on the type of control.
Returning True means the key is intercepted, preventing the key from actually reaching the control at all. This would be useful if you want to override the behavior of the tab key for example. Returning False means the key reaches the control.
DesktopTextArea.KeyUp
KeyUp(Key As String)
Fires when the passed Key is released in the control that has the focus.
It is not guaranteed to be the same key that received the KeyDown event.
DesktopTextArea.MenuBarSelected
MenuBarSelected
Indicates that the control has the focus and a menu (not a menu item) has been selected by the user.
DesktopTextArea.MouseDown
MouseDown(X As Integer, Y As Integer) As Boolean
The mouse button was pressed inside the control's region at the location passed in to x, y.
This event fires repeatedly while the mouse button is being held down.
The coordinates x and y are local to the control, i.e. they represent the position of the mouse click relative to the upper-left corner or the control.
Return True if you are going to handle the MouseDown. In such a case:
The Pressed event, if any, will not execute and the state of the object will not change.
You will receive the MouseUp event.
If you return False, the system handles the MouseDown so the MouseUp event handler do not get called.
The MouseDown event uses the DragItem constructor when the user drags the contents of the control. It is:
Var d As DragItem
d = New DragItem(Self, Me.Left, Me.Top, Me.Width, Me.Height)
d.Picture = Me.Image
d.Drag ' Allow the drag
DesktopTextArea.MouseEnter
MouseEnter
The mouse has entered the area of the control.
DesktopTextArea.MouseExit
MouseExit
The mouse has left the area of the control.
DesktopTextArea.MouseMove
MouseMove(X As Integer, Y As Integer)
The mouse has moved within the control to the coordinates passed. The coordinates are local to the control, not to the window.
DesktopTextArea.MouseUp
MouseUp(X As Integer, Y As Integer)
The mouse button was released. Use the x and y parameters to determine if the mouse button was released within the control's boundaries.
Note
This event will not occur unless you return True in the MouseDown event. The return value is ignored.
The parameters x and y are local coordinates, i.e. they represent the position of the mouse click relative to the upper-left corner or the control. Mouse clicks that are released to the left or above a control are negative.
DesktopTextArea.MouseWheel
MouseWheel(X As Integer, Y As Integer, DeltaX As Integer, DeltaY As Integer) As Boolean
The mouse wheel has been moved.
The parameters X and Y are the mouse coordinates relative to the control that has received the event. The parameters DeltaX and DeltaY hold the number of scroll lines the wheel has been moved horizontally and vertically, as defined by the operating system. DeltaX is positive when the user scrolls right and negative when scrolling to the left. DeltaY is positive when the user scrolls down and negative when scrolling up.
Returns a Boolean. Return True to prevent the event from propagating further.
DesktopTextArea.Opening
Opening
The control is about to be displayed. Use this event to initialize a control.
The Opening event is called after the Constructor.
Be warned that initializing control property values using the Constructor instead of the Opening event may result in those property values being overwritten by what is set in the Inspector. For best results, use the Opening event for control initialization rather than the control Constructor.
If the control is supposed to handle drag and drop, you need to tell it which type of item it needs to be able to handle. The following example informs the control that pictures and files can be dropped on it. The type of the file it needs to support is specified via the File Types Editor.
Sub Opening()
Me.AcceptPictureDrop
Me.AcceptFileDrop("JPEG")
End Sub
DesktopTextArea.SelectionChanged
SelectionChanged
The range of characters highlighted has changed.
DesktopTextArea.TextChanged
TextChanged
The Text property has been changed.
DesktopTextArea.ValidationFailed
ValidationFailed(InvalidText As String, StartPosition As Integer)
The user has tried to enter a character that is prohibited by the Mask property.
InvalidText is the entire character string up to and including the invalid text. StartPosition is the starting character position in which the actual invalid text was entered. The first character is numbered 1. If no code is provided in this event and a validation error occurs, you will hear the default system beep.
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).
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
System.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 = Color.RGB(255, 255, 255)
black = Color.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.
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.
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. |
. |
Decimal placeholder. |
, |
Thousands separator. |
: |
Time separator. |
/ |
Date separator. |
Mask escape character. |
|
& |
Character or space placeholder. |
C |
Character or space placeholder, where entry is optional. It operates like the '&' placeholder. |
> |
Convert all the characters that follow to uppercase. |
< |
Convert all the characters that follow to lowercase. |
A |
Alphanumeric character placeholder, where entry is mandatory. |
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)
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 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 DesktopButton 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.
Compatibility
Desktop projects on all supported operating systems.
See also
DesktopTextControl parent class; Font, System.FontCount functions; Paragraph, Range, StyleRun, StyledText, StyledTextPrinter, DesktopTextControl, DesktopTextField classes.