Copy lines from TextArea

Post

Posted
Rating:
#1 (In Topic #851)
Trainee
Hello everybody.

I want to copy some lines from one TextArea and paste into another.
In the old version of Gambas I used the commands below. But now they don't work anymore.

Code (gambas)

  1. TextArea1.Insert(gb.NewLine & TextArea2.Lines[6].text & gb.NewLine & TextArea2.Lines[7].text & gb.NewLine & TextArea2.Lines[8].text)

Does anyone know how I do this now?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
The insert command will work when the cursor position is a positive integer or zero  (no current text)
TextArea.Pos will set or return the current cursor position.

Code (gambas)

  1.  TextArea.Text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  2.   TextArea.Pos = 0
  3.   TextArea.Insert("bbbbb")
  4.   TextArea.Select(0, 3)
  5.   TextArea.Copy ' To Clipboard
  6.   TextArea.Pos = Txa_Information.Length
  7.   TextArea.Paste 'From Clipboard
  8.  
The output is: bbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb
I'm using MINT 20 with QT and the above example works for me.

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’

kitoeag said

Hello everybody.

I want to copy some lines from one TextArea and paste into another.
In the old version of Gambas I used the commands below. But now they don't work anymore.

Code (gambas)

  1. TextArea1.Insert(gb.NewLine & TextArea2.Lines[6].text & gb.NewLine & TextArea2.Lines[7].text & gb.NewLine & TextArea2.Lines[8].text)

Does anyone know how I do this now?

There's a few ways…

I'd use Split and Extract to get the 3 required lines..

Code (gambas)

  1. TextArea1.Insert(gb.NewLine & Split(TextArea2.Text, "\n").Extract(6, 3).Join("\n"))
  2.  

Or you could make a Line Function..

Code (gambas)

  1. '' Returns the line at the given Index
  2.  
  3.   Return Split(Text, "\n")[Index]
  4.  
  5.  
  6.  
then instead of TextArea2.Lines[6].text use
Line(TextArea2.Text, 6)

Or as you are getting lines 6, 7, and 8 you could join the 2 above methods to be a function called Lines() that will get more than one line joined with LF's…

Code (gambas)

  1.  
  2. '' Returns "Length" amount of lines as a string from the given Index.
  3.  
  4.  
  5.   Return Split(Text, "\n").Extract(Index, Length).Join("\n")
  6.  
  7.  
  8.  
So you could write…
TextArea1.Insert(gb.NewLine & Lines(TextArea2.Text, 6, 3))

Or you could use an array of indexes in case the required lines were not all together…

Code (gambas)

  1.  
  2. '' Returns the lines as a string from the given Index array.
  3.  
  4. Public Sub Lines(Text As String, Indexes As Integer[]) As String
  5.   Dim sText As String[] = Split(Text, "\n")
  6.   Dim sNew As New String[]
  7.     For Each Index As Integer In Indexes
  8.      sNew.Add(sText[Index])
  9.     Next  
  10.  
  11.   Return sNew.Join("\n")
  12.  
  13.  
  14.  
So you could write…
TextArea1.Insert(gb.NewLine & Lines(TextArea2.Text, [6, 7,10]))
Online now: No Back to the top
1 guest and 0 members have just viewed this.