User Tools

Site Tools


k16:k16.14:k16.16.1:start

16.14.1 Project - Find and replace text in a TextArea

Finding and replacing text is also very good in a TextArea with regular expressions. See Chapter 19.6.6.6 Finding and replacing strings in a text for more information.

In this chapter, however, the emphasis was put on working with methods and properties of the TextArea class in the presented project when searching for and replacing text.

  • You can select the search text in the TextArea and transfer it to the search text line by clicking on the three dots in the yellow ButtonBox. You can also enter the search text directly in the search text line. Figure 16.14.1.1.1 If no or no other location exists, will you be notified by a MessageBox.
  • To replace text, it is necessary to specify the search text and the replacement text in the corresponding TextBoxes. All replacements are made and only the first replacement is selected. If the search text cannot be found in the text, you will be informed that nothing has been replaced.

Figure 16.14.1.1: Searching for text in a TextArea

16.14.1.1.1 Project - Source code

The source text is completely specified and then commented on:

[1] ' Gambas class file
[2] 
[3] Private $iPos As Integer
[4] 
[5] Public Sub Form_Open()
[6]   Dim sMimeTypeCharSet, sCharSet, sMime As String 
[7]   Dim sFilePath As String
[8]   
[9]   FMain.Center
[10]   btnNextSearch.Tooltip = ("Suchen")
[11]   btnReplace.Tooltip = ("Ersetzen")
[12]   
[13]   txaArea.Background = &HF5FFE6& ' Text-Hintergrund-Farbe: hellgrün
[14]   txaArea.Foreground = Color.RGB(0, 0, 0) ' Schrift-Farbe: schwarz
[15]   txaArea.Font = Font["Monospace, 10"] ' dicktengleiche Schrift
[16] ' Print txaArea.Font.ToString()
[17] 
[18]   ' Dialog.Title = "Wählen Sie eine Text-Datei aus!"
[19]   ' Dialog.Filter = ["*.ini", "INI-Dateien", "*.txt", "Text-Dateien", "*", "Alle Dateien"]
[20]   ' Dialog.Path = "/etc/odbcinst.ini"
[21]   ' If Dialog.OpenFile() Then Return
[22]   ' sFilePath = Dialog.Path
[23] 
[24]   sFilePath = Application.Path &/ "odbcinst.ini" ' Beispiel-Textdatei im Projekt-Verzeichnis
[25] 
[26]   Exec ["file", "-bi", sFilePath] To sMimeTypeCharSet ' Ermittlung MimeTyp und Zeichensatz 
[27]   Wait
[28] ' Print sMimeTypeCharSet
[29] ' sMime = Split(sMimeTypeCharSet, ";")[0]
[30] ' Print "Mime-Typ = "; Trim(sMime)  
[31]   sCharSet = Trim(Split(Split(sMimeTypeCharSet, ";")[1], "=")[1])
[32] ' Print "CharSet  = "; Trim(sCharSet)
[33]   
[34]   If sCharSet <> "utf-8" Then
[35]      Try txaArea.Text = Conv(File.Load(sFilePath), sCharSet, "UTF-8")
[36]   Else
[37]      txaArea.Text = File.Load(sFilePath) 
[38]   Endif
[39]   
[40]   txaArea.Pos = 0
[41]   
[42] End ' Form_Open()
[43] 
[44] Public Sub txbSearch_Click()
[45]   txbSearch.Text = txaArea.Selection.Text ' Markierter Text = Suchtext
[46] End
[47] 
[48] Public Sub txbSearch_Change()
[49]   $iPos = -1 ' Ab Text-Anfang suchen
[50] End
[51] 
[52] Public Sub btnReplace_Click()
[53]   ReplaceText()
[54] End
[55] 
[56] Public Sub btnNextSearch_Click()
[57]   If Not txbSearch.Text Then
[58]      Message.Warning(("Es fehlt der Suchtext!"))
[59]      txbSearch.SetFocus
[60]      Return
[61]   Endif
[62]   txaArea.Unselect
[63]   SearchText()
[64] End
[65] 
[66] '---------------------------------------------------------------------------------------
[67] 
[68] Private Sub SearchText()
[69]   Dim iPos As Integer
[70] 
[71]   iPos = String.InStr(txaArea.Text, txbSearch.Text, $iPos + 2)
[72]   $iPos = iPos - 1  
[73]   If Not iPos Then
[74]     Message(("Suchtext nicht gefunden!"))
[75]     txaArea.SetFocus
[76]     txaArea.Pos = 0
[77]     Return
[78]   Endif
[79]   txaArea.Select($iPos, String.Len(txbSearch.Text)) ' Markierung einer Fundstelle im Text
[80]   
[81] End ' SearchText(...)
[82] 
[83] Private Sub ReplaceText()
[84]   Dim iPos As Integer
[85]   Dim bNotReplaced As Boolean = True
[86] 
[87]   If Not txbSearch.Text Then
[88]      Message.Warning(("Es fehlt der Suchtext!"))
[89]      Return
[90]   Endif
[91]   If Not txbReplace.Text Then
[92]      Message.Warning(("Es fehlt der Ersetzungstext!"))
[93]      txbReplace.SetFocus
[94]      Return
[95]   Endif
[96]   While String.InStr(txaArea.Text, txbSearch.Text)
[97]     txaArea.Text = Replace(txaArea.Text, txbSearch.Text, txbReplace.Text)
[98]     bNotReplaced = Not bNotReplaced
[99]   Wend
[100] 
[101]   iPos = String.InStr(txaArea.Text, txbReplace.Text, 0)
[102]   If Not iPos And bNotReplaced Then
[103]      Message(("Es wurde NICHTS ersetzt!"))
[104]      Return
[105]   Endif
[106] 
[107]   txaArea.Select(iPos - 1, String.Len(txbReplace.Text))
[108]   
[109] End ' ReplaceText()

Comment:

  • The background color, font color and font are defined globally in lines 13-15 for the TextArea.
  • You can use the lines 18-22 to implement a file open dialog. To do this, you would have to activate lines 18-22 and comment out line 24. This is not used here to work with a special file that is in the project path.
  • In order to guarantee that a UTF-8 string is inserted into the TextArea, the character set of the (text) file to be imported is determined in lines 26 and 31. conversion only takes place if this is necessary (lines 34-38).
  • In line 79 the temporary marking of a find is created.
  • The main load in the project is carried by the procedure SearchText () in lines 68-81 and the procedure ReplaceText() in lines 83-109.

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/k16.16.1/start.txt · Last modified: 29.09.2023 by emma

Page Tools