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]))