User Tools

Site Tools


k16:k16.14:start

16.14.0 TextArea

The class TextArea (gb. qt4) implements a multiline text field.

WT

Figure 16.14.0.1: Value table in a TextArea

16.14.0.1 Properties

Selected properties of the TextArea class are described in the following table:

PropertyTypeDescription
AlignmentIntegerReturns or sets the text alignment. The following constants can be used: Align.Normal (0), Align.Left (1), Align.Right (2) or Align.Center (3).
BackgroundIntegerSets the background color or reads the color value.
ForegroundIntegerSets the text color or reads the current text color.
FontFontSets the text font or reads the current text font.
ColumnIntegerReturns the text column in the current text line or sets the defined text column.
LineIntegerReturns the current text line or sets the defined text line.
PosIntegerReturns the current cursor position or sets the cursor to the defined position in the text. If the text was saved in UTF-8, you must use the string class to convert the position to the byte index in the string.
DropBooleanDetermines or specifies whether the TextArea accepts the insertion of text by drag & drop.
LengthIntegerReturns the length of the text as the number of characters in the text.
ReadOnlyBooleanDetermines or determines whether the user can change the text.
SelectedBooleanReturns True if text is selected.
TextStringReturns or sets the text displayed in the TextArea.
WrapBooleanDetermines or determines whether the text is wrapped depending on the width of the TextArea. There is no hyphenation!

Table 16.14.0.1.1.1: Overview of selected properties of the TextArea class (gb. qt4)

Hints:

  • The properties Pos, Column and Line are easier to access if you imagine the text in a TextArea as a character string into which “\n” or gb. NewLine end-of-line characters (EOL) have been inserted.
  • The Column property is a relative size and always refers to the current text line! If you see the Column property as a cursor position in a text line, you are right.
  • The counting of rows, columns and the cursor position starts at 0.

Only the two properties Foreground and Font are available for marking the text in a TextArea. They always apply globally for the entire TextArea!

  txaArea.Foreground = Color.RGB(0, 0, 0) ' Font color black
  txaArea.Font = Font["Monospace, 10"]    ' calligraphic font
' Zur Kontrolle:
' Print txaArea.Font.ToString()

The overview of the selected methods of the TextArea class contains three methods for reciprocal conversion of values of the properties Pos, Column and Line.

  Print "Zeile = "; txaTest.ToLine(91)
  Print "Spalte = "; txaTest.ToColumn(91)
  Print "Cursor-Position = "; txaTest.ToPos(1, 30)

The property TextArea. ReadOnly = True is used if text is only to be displayed and manual text changes in the text field are not allowed. Examples: Help texts, display of measured values, console outputs or similar…

16.14.0.2 Methods

A description of selected methods of the class TextArea can be found here:

MethodDescription
ClearDeletes the entire text.
CopyThe selected text is copied to the ClipBoard.
CutThe selected text is cut out and inserted into the ClipBoard.
PasteInserts the (text) content of the ClipBoard at the cursor position.
Insert (text As String)Inserts the specified text from the current cursor position into the TextArea.
Select ([ Start As Integer, Length As Integer])Marks text from cursor position Start in the length of Length.“ Start is the position of the first selected character in the text.” Length is the length of the text to be selected. If no argument is specified, the entire text is selected.
SelectAllSelects the entire text.
UnselectResets the selection of the text.
ToColumn (Pos As Integer) As IntegerConverts a cursor position to a column number in a text line? method ToLine (..).
ToLine (Pos As Integer) As IntegerConverts a cursor position into a line number.
ToPos (Line As Integer, Column As Integer) As IntegerConverts a combination of text line number and text column number (in this text line!) into a cursor position.
UndoReverses the last action.
RedoResets the last undone action.

Table 16.14.0.2.1: Selected methods of the TextArea class (gb. qt4)

Hint:

  • You will find interesting information about the methods Copy(), Cut() and Paste() in the chapters 20.4.0' Clipboard' and 20.4.1' Project 1 - Demonstration program Clipboard Text'.

16.14.0.3 Events

These two events are particularly important for the practical work with a TextArea:

EventDescription
ChangeIs triggered when the text has been changed.
CursorThe event is triggered when the cursor position has changed.

Table 16.14.0.3.1: Overview of selected events of the TextArea class (gb. qt4)

Hints:

Note that changing the text does not necessarily change the cursor position, but the example Gambas program Notepad (text editor) implements the above mentioned properties, methods and events very well, so that it will be worthwhile for you to test this example intensively in any case.

16.14.0.4 Context menu

The context menu of a TextArea, together with the short cuts - even without menus and toolbars - guarantees a fast working with texts:

KM

Figure 16.14.0.4.1: Context menu

16.14.0.5 Inserting text

You can insert text into a TextArea at any cursor position in several ways:

  1. Text input via keyboard
  2. Insert text from the ClipBoard → context menu
  3. Insert text via drag&drop from other suitable (text) sources
  4. Import the content of a text file (text import)
  5. Modify the TextArea.Text text property
  6. Use the TextArea. insert (“text”) method

Inserting Text Using Drag&Drop

Inserting text using drag & drop was successfully tested in the editors 'gedit', 'bluefish' and in the Internet browser 'firefox'. The selected text in the source is dragged over the text field while holding the mouse button down. Then the mouse button is released and the selected text is located in the TextArea and can be edited there.

Text import from a text file

Minisini said on this topic in a forum that you can only enter UTF-8 strings in a TextArea and emphasized: “If another character set works…, then the benevolence of QT or coincidence.

With this section of source code you are always on the right page:

[1]   ...
[2]   Dim sMimeTypeCharSet, sCharSet, sMime As String 
[3]   Dim sFilePath As String
[4] 
[5]   Dialog.Title = "Wählen Sie eine Text-Datei aus!"
[6]   Dialog.Filter = ["*.ini", "INI-Dateien", "*.txt", "Text-Dateien", "*", "Alle Dateien"]
[7]   Dialog.Path = "/etc/odbcinst.ini"
[8]   If Dialog.OpenFile() Then Return
[9]   sFilePath = Dialog.Path
[10]   
[11]   Exec ["file", "-bi", sFilePath] To sMimeTypeCharSet ' Ermittlung MimeTyp und Zeichensatz 
[12]   Wait
[13]   Print sMimeTypeCharSet
[14]   sMime = Split(sMimeTypeCharSet, ";")[0]
[15]   Print "Mime-Typ = "; Trim(sMime)  
[16]   sCharSet = Trim(Split(Split(sMimeTypeCharSet, ";")[1], "=")[1])
[17]   Print "CharSet  = "; Trim(sCharSet)
[18]   
[19]   If sCharSet <> "utf-8" Then
[20]      Try txaArea.Text = Conv(File.Load(sFilePath), sCharSet, "UTF-8")
[21]    ' Try txaArea.Insert(Conv(File.Load(sFilePath), sCharSet, "UTF-8"))
[22]   Else
[23]      txaArea.Text = File.Load(sFilePath) 
[24]    ' txaArea.Insert(File.Load(sFilePath))
[25]   Endif
[26]   
[27]   txaArea.Pos = 0
[28]   ...

Comment:

  • The text file to be imported is selected in lines 5-9 of a dialog.
  • In line 11 the combined MimeType character set value of the imported text file is read and stored in the variable sMimeTypeCharSet.
  • Line 16 extracts the character set needed in line 20 to convert the character set - if necessary.
  • Attention: The imported text in line 20 or 23 may replace the already existing text.
  • However, the imported text is appended to the end of the text by default if you use lines 21 or 24.
  • The blue highlighted lines are for control purposes only and can be commented out.

The configuration file odbcinst. ini provided the following combined value from mime type and character set:

text/plain; charset=us-ascii

The character set of the file was therefore converted to UTF-8 according to the requirement in line 19. This was not necessary in the case of us-ascii because ASCII is included in UTF-8.

Text changes using the Text Property and the Insert(..) method

In this section, on the one hand, text is inserted into a TextArea, in which a change of the property TextArea.Text = text string the old text is completely overwritten by the new text and on the other hand, it is shown how to add text at the current text cursor position:

TextArea.Text = "Ich bin der neue Text!"
TextArea.Text = TextArea.Text & "Ich bin weiterer Text!"
TextArea.Text &= "Ich bin weiterer Text!"
TextArea.Text &= gb.NewLine ' erzwingt einen Zeilenwechsel
TextArea.Text &= gb.NewLine & gb.NewLine ' erzeugt eine Leerzeile nach dem Text 
TextArea.Insert("Ich bin Text – an der aktuellen Cursor-Position eingefügt!")

Text export

If you want to save text via the ClipBoard, this only creates an apparent security, because the content of the ClipBoard is automatically deleted when you exit the program with the TextArea!

The easiest way to save the contents of a TextArea is to use a file save dialog:

Public Sub btnTextToFile_Click() 
  Dialog.Filter = ["*.txt", "Text-Dateien"] 
  If Dialog.SaveFile() Then Return 
  File.Save(Dialog.Path, txaArea.Text) 
  CATCH 
    Message.Info(Error.Text) 
END

Useful information

The following commented source code sections are helpful when working with a TextArea:

Print "Number of characters = "; txaTest.Length
Print "Number of lines = "; Split(txaTest.Text, gb.NewLine).Count
Print "Number of lines = "; txaTest.ToLine(txaTest.Length) + 1
Public Sub btnSetWrapping_Click()
  txaTest.Wrap = Not txaTest.Wrap ' Alternative change of the wrap property (line break)
End
TextArea.Text = "" ' deletes the entire text 
TextArea.Clear ' deletes the entire text 
Public Sub txaTest_Cursor()
  Print "Zeile = "; txaTest.Line
  Print "Spalte = "; txaTest.Column
  Print "Position = "; txaTest.Pos
  Print "Cursor in row "; txaTest.Line; " in column "; txaTest.Column; " at position "; txaTest.Pos
End
txaTest.Pos = 0 ' Jump to the first line (physical 1) to column 0
txaTest.Pos = txaTest.Length ' Jump to the last line 
Public Sub txaTest_Cursor()
  lblAktuelleZeile.Text = "Z: " & txaTest.Line     ' Display of the current line 
  lblAktuelleSpalte.Text = "S: " & txaTest.Column  ' Display of the current column (in the row)
  lblCursorPosition.Text = "P: " & txaTest.Pos     ' Display of the current cursor position
End

Limited Text

An SMS normally comprises a maximum of 160 characters. You can guarantee this number of characters with this section of the source code. Also the case that there is too much text in the TextArea by copying and pasting has been taken into account:

Public Const MAX_CHAR As Integer = 160
 
Public Sub txaTest_Change()
 
' If Len(Last.Text) < MAX_CHAR Then ' Alternative
  If txaTest.Length < MAX_CHAR Then
     txaTest.Foreground = &00007FFF&
     If MAX_CHAR - txaTest.Length = 1 Then
        FMain.Text = Subst$("Exactly 1 character is still available!") 
     Else
        FMain.Text = Subst$("There are still &1 characters available!", MAX_CHAR - txaTest.Length) 
     Endif
  Else If txaTest.Length = MAX_CHAR
     txaTest.Foreground = &00007FFF&
     txaTest.ReadOnly = True
     FMain.Text = Subst$("The maximum number of characters " & Str(MAX_CHAR) & " has been reached!") 
  Else
    txaTest.Foreground = Color.Red
    FMain.Text = Subst$("&1 characters are inserted too many.", - MAX_CHAR + txaTest.Length) 
  Endif
 
End ' txaTest_Change()
...
Public Sub txaTest_KeyPress()
  If Key.Code = Key.BackSpace Or Key.Code = Key.Delete Then txaTest.ReadOnly = False  
End ' txaTest_KeyPress()

The topic 'Finding and replacing text in a TextArea' is dealt with in the next chapter.

Download

Articles

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.14/start.txt · Last modified: 12.10.2023 by emma

Page Tools