Page 1 of 1

Copy lines from TextArea

Posted: Monday 23rd May 2022 10:05pm
by kitoeag
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.
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?

Re: Copy lines from TextArea

Posted: Tuesday 24th May 2022 2:04am
by Quincunxian
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.
 TextArea.Text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  TextArea.Pos = 0
  TextArea.Insert("bbbbb")
  TextArea.Select(0, 3)
  TextArea.Copy ' To Clipboard
  TextArea.Pos = Txa_Information.Length
  TextArea.Paste 'From Clipboard
 
The output is: bbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb
I'm using MINT 20 with QT and the above example works for me.

Re: Copy lines from TextArea

Posted: Thursday 2nd June 2022 2:42am
by BruceSteers
kitoeag wrote: Monday 23rd May 2022 10:05pm 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.
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..
TextArea1.Insert(gb.NewLine & Split(TextArea2.Text, "\n").Extract(6, 3).Join("\n"))
Or you could make a Line Function..
'' Returns the line at the given Index
Public Sub Line(Text As String, Index As Integer) As String

  Return Split(Text, "\n")[Index]

End

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

'' Returns "Length" amount of lines as a string from the given Index.

Public Sub Lines(Text As String, Index As Integer, Optional Length As Integer = 1) As String

  Return Split(Text, "\n").Extract(Index, Length).Join("\n")

End

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

'' Returns the lines as a string from the given Index array.

Public Sub Lines(Text As String, Indexes As Integer[]) As String
  Dim sText As String[] = Split(Text, "\n")
  Dim sNew As New String[]
    For Each Index As Integer In Indexes
     sNew.Add(sText[Index])
    Next  

  Return sNew.Join("\n")

End

So you could write...
TextArea1.Insert(gb.NewLine & Lines(TextArea2.Text, [6, 7,10]))