Copy lines from TextArea

Post your Gambas programming questions here.
Post Reply
kitoeag
Posts: 1
Joined: Monday 23rd May 2022 9:47pm

Copy lines from TextArea

Post 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?
User avatar
Quincunxian
Posts: 171
Joined: Sunday 25th June 2017 12:14am
Location: Western Australia

Re: Copy lines from TextArea

Post 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.
Cheers - Quin.
I code therefore I am
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Copy lines from TextArea

Post 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]))
If at first you don't succeed , try doing something differently.
BruceS
Post Reply