The TextEdit control implements a RichText editor, whereby the term RichText in this context has nothing to do with the well-known RichText format.
How to create a new TextEdit editor:
Dim hTextEdit As TextEdit hTextEdit = New TextEdit ( Parent As Container ) [ As "EventName" ]
Gambas RichText is a character string and is based on a special markup that uses a subset of HTML to mark up text.
The following HTML tags are allowed for Gambas RichText to mark up text elements:
<h1>, <h2>, <h3>, <h4>, <h5>, <h6> → headings <b> → bold, <i> → italic, <s> → strikethrough, <u> → underlined <sub> → subscript, <sup> → superscript, <small> → small, <p> → paragraph <br> → line break, <a> → link, <font> → font
Information about the <font> tag is given in the following table. Please note that the <font> tag no longer exists under HTML5!
Font attribute | Values | Notes |
---|---|---|
face | Font family | Different fonts can be specified in a comma-separated list |
color | Color values via color names or as hexadecimal numbers or in RGB format | color = Red, color = #C3DDFF → light blue, color = RGB(255,175,255,95) → orange |
size | Font size (Default = 3) | Absolute values from 1 to 7 or the two relative values -1 for smaller font or +1 for larger Font |
Table 16.15.1.1: Information on the <font> tag
These tags have also been tried and tested for marking up single or multi-line source text or for inserting a horizontal line or displaying an unsorted list with n entries:
<tt> → single-line source code, <pre> → multi-line source code, <hr> → horizontal line <ul> → unsorted list, <li> → list entry
Notes:
The TextEdit class has these properties:
Property | Data type | Description |
---|---|---|
Border | Boolean | Specifies whether the control has a border or sets one. |
Format | .TextEdit.Format | The virtual class .TextEdit.Format is used to format the currently selected text (or the current paragraph if no text is selected). A paragraph is marked with <p>TEXT</p>. |
Index | Integer | Returns or sets the position of the current character within the current paragraph. |
Paragraph | Integer | Returns or sets the index of the current paragraph, where the first paragraph has the index 0. |
Pos | Integer | Returns or sets the cursor position relative to the beginning of the text. If a text is stored in UTF-8, you must use the String class to convert this position to the byte index of the string. |
ReadOnly | Boolean | Indicates or sets whether the user can modify the text. |
RichText | String | Returns or sets the RichText of the editor. |
ScrollBar | Integer | Returns or sets which scroll bars are displayed. For a list of constants, see the Scroll class. |
ScrollX | Integer | Gets or sets the left position of the editor scroll area. |
ScrollY | Integer | Gets or sets the top position of the editor scroll area. |
Selected | Boolean | Returns whether a text section is selected. |
Selection | .TextEdit.Selection | Returns a virtual object used to manage the selected text is. |
Text | String | Returns or sets the pure text content of the editor. |
TextHeight | Integer | Returns the height of the rich text displayed in the rich text editor. |
TextWidth | Integer | Returns the width of the rich text displayed in the control. |
Wrap | Boolean | Returns or sets whether the text is wrapped to the width of the editor. |
Table 16.15.2.1: Properties of the TextEdit class
Property | Data type | Description |
---|---|---|
Alignment | Integer | Sets or returns the alignment of the current paragraph or the currently selected text. |
Background | Integer | Reads or sets the background color of the currently selected text. |
Color | Integer | Reads or sets the foreground color of the currently selected text. |
Font | Font | Reads or sets the font of the currently selected text. |
Table 16.15.2.1.1: Properties of the TextEdit.Format class
Setting the 'TextEdit.Foreground' property has no effect on the text in the RichText editor.
You must either use HTML syntax to set text colors `inline` or set the text color using the TextEdit.Format.Color property of the TextEdit.Format virtual class:
TextEdit1.RichText = "<font color=red>This is some red text</font> and the text is default text color"
The same result can be achieved using inline CSS:
TextEdit1.RichText = "<span style=\"color: red;\">This is some red text</span> and the text is the default text color."
Alternative without HTML syntax:
TextEdit1.Format.Color = Color.Red TextEdit1.Insert("This is some red text") TextEdit1.Format.Color = Color.Default TextEdit1.Insert(" and the text is default text color")
Property | Data type | Description |
---|---|---|
Length | Integer | Returns the length of the selected text as a number of characters |
RichText | String | Returns the current selected text as RichText or replaces the current selected text with the specified RichText. |
Start | Integer | Returns the starting position of the currently selected text. |
Text | Text | Returns the current selected text as plain text or replaces the current selected text with the specified plain text. |
Table 16.15.2.2.1: Properties of the TextEdit.Selection class
The TextEdit.Selection.Hide( ) method removes a selection of the currently selected text.
The TextEdit class has these methods:
Method | Return value | Description |
---|---|---|
Clear ( ) | - | Deletes the (complete) text in the editor. |
Copy ( ) | - | Copies the selected text to the clipboard. |
Cut ( ) | - | Cuts the selected text and pastes it to the clipboard. |
EnsureVisible ( ) | - | This ensures that the cursor is visible. If necessary, an automatic scrolling process is triggered. |
Insert ( Text As String ) | - | Inserts the text specified in the parameter at the current cursor position. |
InsertRichText ( RichText As String ) | - | Inserts the specified RichText at the current cursor position. |
Paste ( ) | - | Pastes the contents of the clipboard at the current cursor position. |
Redo ( ) | - | Repeat the last undone (text) action. |
Undo ( ) | - | Undoes the last (text) action. |
Select ( [ Start As Integer, Length As Integer ] ) | - | Defined the selected text. Start is the position of the first selected character in the text. Length is the length of the selection. If no argument is specified, the entire text is selected. |
SelectAll ( ) | - | Select the entire text. |
ToIndex ( Pos As Integer ) | Integer | Converts a position to a character index within a paragraph. |
ToParagraph ( Pos As Integer ) | Integer | Converts a position to a paragraph number (index). |
ToPos ( Paragraph As Integer, Index As Integer ) | Integer | Converts a paragraph number and a character index within the paragraph to a Integer position. |
Table 16.15.3.1: Methods of the TextEdit class
The TextEdit class has these two relevant events:
Event | Description |
---|---|
Change ( ) | The event is triggered when the text in the editor has changed. |
Cursor ( ) | The event is triggered when the cursor position has changed. |
Table 16.15.4.1: Events of the TextEdit class
===== 16.15.5 Project <x h>======</x>
As an example of a TextEdit editor, reference is made to the project from the Gambas sources. You can also find the project archive in the download area.
Figure 16.15.5.1: GUI: TextEdit editor