User Tools

Site Tools


k16:k16.15:start

16.15 TextEdit (gb.qt4.ext)

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" ]

16.15.1 Syntax of RichText in Gambas

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 attributeValuesNotes
faceFont familyDifferent fonts can be specified in a comma-separated list
colorColor values ​​via color names or as hexadecimal numbers or in RGB formatcolor = Red, color = #C3DDFF → light blue, color = RGB(255,175,255,95) → orange
sizeFont 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:

  • Working with CSS is supported both internally and externally, but you should not expect that all design options offered by a browser can also be implemented with the TextEdit control.
  • Even though Gambas-RichText primarily uses the HTML tags mentioned above as basic markup, you can use any HTML entity.
  • You can use <a>…</a> to insert a link into the text. It is underlined and formatted in blue - as expected for a hyperlink - but it has no effect! After right-clicking on the link, you can copy the link address from the context menu.

16.15.2 Properties

The TextEdit class has these properties:

PropertyData typeDescription
BorderBooleanSpecifies whether the control has a border or sets one.
Format.TextEdit.FormatThe 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>.
IndexIntegerReturns or sets the position of the current character within the current paragraph.
Paragraph IntegerReturns or sets the index of the current paragraph, where the first paragraph has the index 0.
PosIntegerReturns 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.
ReadOnlyBooleanIndicates or sets whether the user can modify the text.
RichTextStringReturns or sets the RichText of the editor.
ScrollBarIntegerReturns or sets which scroll bars are displayed. For a list of constants, see the Scroll class.
ScrollXIntegerGets or sets the left position of the editor scroll area.
ScrollYIntegerGets or sets the top position of the editor scroll area.
SelectedBooleanReturns whether a text section is selected.
Selection .TextEdit.SelectionReturns a virtual object used to manage the selected text is.
TextStringReturns or sets the pure text content of the editor.
TextHeightIntegerReturns the height of the rich text displayed in the rich text editor.
TextWidthIntegerReturns the width of the rich text displayed in the control.
WrapBooleanReturns or sets whether the text is wrapped to the width of the editor.

Table 16.15.2.1: Properties of the TextEdit class

16.15.2.1 Properties of the virtual class TextEdit.Format

PropertyData typeDescription
Alignment IntegerSets or returns the alignment of the current paragraph or the currently selected text.
BackgroundIntegerReads or sets the background color of the currently selected text.
ColorIntegerReads or sets the foreground color of the currently selected text.
Font FontReads 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")

16.15.2.2 Properties of the TextEdit.Selection virtual class

PropertyData typeDescription
LengthIntegerReturns the length of the selected text as a number of characters
RichTextStringReturns the current selected text as RichText or replaces the current selected text with the specified RichText.
StartIntegerReturns the starting position of the currently selected text.
TextTextReturns 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.

16.15.3 Methods

The TextEdit class has these methods:

MethodReturn valueDescription
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 ) IntegerConverts a position to a character index within a paragraph.
ToParagraph ( Pos As Integer ) IntegerConverts a position to a paragraph number (index).
ToPos ( Paragraph As Integer, Index As Integer ) IntegerConverts a paragraph number and a character index within the paragraph to a Integer position.

Table 16.15.3.1: Methods of the TextEdit class

16.15.4 Events

The TextEdit class has these two relevant events:

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

GUI

Figure 16.15.5.1: GUI: TextEdit editor

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k16/k16.15/start.txt · Last modified: 04.01.2025 (external edit)

Page Tools