TextArea - Selecting text with code

Post your Gambas programming questions here.
Post Reply
bazzvn
Posts: 12
Joined: Wednesday 22nd February 2017 11:06am
Location: Vietnam

TextArea - Selecting text with code

Post by bazzvn » Wednesday 14th July 2021 9:43am

I am trying to select a line of text in a TextArea programmatically, but I must have missed something because I keep getting an error. As an example, I have a form with two text areas, and I want to transfer some text from one to the other. Here is the code I have tried.

Code: Select all

Public Sub Form_Open()

  Dim i As Integer
  Dim s As String = Null
  
  For i = 0 To 9
    If s Then
      s &= gb.NewLine & i & ". This is line " & i
    Else
      s = i & ". This is line " & i
    Endif
  Next
  TextArea1.Text = s

End

Public Sub TextArea1_MouseUp()
  
  Dim iLin, iCol, iFirst As Integer
  Dim sText As String
  
  iLin = TextArea1.Line
  iCol = TextArea1.Column
  iFirst = TextArea1.ToPos(iLin, 0)
  
  sText = "Current cursor position = " & TextArea1.Pos
  sText &= gb.NewLine & "Current line = " & iLin & ";   Current column = " & iCol
  sText &= gb.NewLine & "Position at start of line " & iLin & " = " & iFirst
  sText &= gb.NewLine & TextArea1.Select(iFirst, 1) ''####### THIS IS THE PROBLEM LINE
  TextArea2.Text = sText
  
End

The error arises in trying to select the text with TextArea1.Select(iFirst,1). It gives the error "No return value in FMain.32.
According to the Gambas wiki, Textarea.Select should select the text with a start and length range:
TextArea.Select (gb.qt4)

Sub Select ( [ Start As Integer, Length As Integer ] )

Define the selected text.
Start is the position of the first selected character in the text.
Length is the length of the selection.
If no argument is specified, the entire text is selected.

So what am I missing or doing wrong?

bazzvn

User avatar
BruceSteers
Posts: 657
Joined: Thursday 23rd July 2020 5:20pm

Re: TextArea - Selecting text with code

Post by BruceSteers » Wednesday 14th July 2021 9:55am

bazzvn wrote:
Wednesday 14th July 2021 9:43am
I am trying to select a line of text in a TextArea programmatically, but I must have missed something because I keep getting an error. As an example, I have a form with two text areas, and I want to transfer some text from one to the other. Here is the code I have tried.

Code: Select all

Public Sub Form_Open()

  Dim i As Integer
  Dim s As String = Null
  
  For i = 0 To 9
    If s Then
      s &= gb.NewLine & i & ". This is line " & i
    Else
      s = i & ". This is line " & i
    Endif
  Next
  TextArea1.Text = s

End

Public Sub TextArea1_MouseUp()
  
  Dim iLin, iCol, iFirst As Integer
  Dim sText As String
  
  iLin = TextArea1.Line
  iCol = TextArea1.Column
  iFirst = TextArea1.ToPos(iLin, 0)
  
  sText = "Current cursor position = " & TextArea1.Pos
  sText &= gb.NewLine & "Current line = " & iLin & ";   Current column = " & iCol
  sText &= gb.NewLine & "Position at start of line " & iLin & " = " & iFirst
  sText &= gb.NewLine & TextArea1.Select(iFirst, 1) ''####### THIS IS THE PROBLEM LINE
  TextArea2.Text = sText
  
End

The error arises in trying to select the text with TextArea1.Select(iFirst,1). It gives the error "No return value in FMain.32.
According to the Gambas wiki, Textarea.Select should select the text with a start and length range:
TextArea.Select (gb.qt4)

Sub Select ( [ Start As Integer, Length As Integer ] )

Define the selected text.
Start is the position of the first selected character in the text.
Length is the length of the selection.
If no argument is specified, the entire text is selected.

So what am I missing or doing wrong?

bazzvn
TextArea.Select does not return anything.
by putting it in your string message it is looking for a return value. it makes no sense why you have done that?
Just remove that last line.
 iFirst = TextArea1.ToPos(iLin, 0)
  
  sText = "Current cursor position = " & TextArea1.Pos
  sText &= gb.NewLine & "Current line = " & iLin & ";   Current column = " & iCol
  sText &= gb.NewLine & "Position at start of line " & iLin & " = " & iFirst
  TextArea2.Text = sText
 
 TextArea1.Select(iFirst, 1) ''####### THIS IS THE PROBLEM LINE
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
cogier
Site Admin
Posts: 761
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: TextArea - Selecting text with code

Post by cogier » Wednesday 14th July 2021 2:02pm

This may not do everything you require, but I hope it points you in the right direction.
Public Sub Form_Open()

  For iLoop As Integer = 0 To 9
    TextArea1.Text &= Str(iLoop) & ". This is line " & Str(iLoop) & gb.NewLine
  Next

End

Public Sub TextArea1_MouseUp()

  TextArea1.Copy
  TextArea2.Text &= Clipboard.Paste() & gb.NewLine

End
TIP: - Use the gb button in the forum not the </> button for your code.

Image

bazzvn
Posts: 12
Joined: Wednesday 22nd February 2017 11:06am
Location: Vietnam

Re: TextArea - Selecting text with code

Post by bazzvn » Wednesday 14th July 2021 3:36pm

Thank you Bruce and Cogier. Maybe I wasn't very clear. Basically what I want to do is use a single mouse right-click somewhere in a line of text to select the whole line (which could be wrapped within the control) and then use a popup menu to provide options for processing it further. It should be possible to select all the text between the start of a line and the next gb.NewLine using TextArea.Line, TextArea.Column and TextArea.ToPos, by looping character by character from the start of a line to the next gb.NewLine. The only issue is how to return each character in the loop so it can be added to the return string. I couldn't see any any obvious way to do that other than TextArea.Select, so I'm still not sure how to proceed.

User avatar
BruceSteers
Posts: 657
Joined: Thursday 23rd July 2020 5:20pm

Re: TextArea - Selecting text with code

Post by BruceSteers » Wednesday 14th July 2021 11:39pm

How's this....
[EDIT: handle if on last line]
Public Sub TextArea1_MouseUp()

  ' Set start pos to beginning of the line
  Dim iStartPos As Integer = TextArea1.ToPos(TextArea1.Line, 0)

 ' get pos of next "\n"
  Dim iLF As Integer = InStr(TextArea1.Text, "\n", iStartPos + 1)

' length is LF pos - 1 (or end of file) minus start pos
  Dim iLength As Integer = Iif(iLF = 0, TextArea1.Text.Len, iLF - 1) - iStartPos

  TextArea1.Select(iStartPos, iLength)  ' select startpos and line length

End

Last edited by BruceSteers on Thursday 15th July 2021 12:17am, edited 1 time in total.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 657
Joined: Thursday 23rd July 2020 5:20pm

Re: TextArea - Selecting text with code

Post by BruceSteers » Thursday 15th July 2021 12:13am

Ps.

you will have trouble using right mouse click as textarea want to pop up it's copy/paste menu on right click.
A better option it to use an Alt or Control key to do it..
Public Sub TextArea1_MouseUp()

  If Mouse.Alt Then
    Dim iStartPos As Integer = TextArea1.ToPos(TextArea1.Line, 0)
    Dim iLF As Integer = InStr(TextArea1.Text, "\n", iStartPos + 1)
    Dim iLength As Integer = Iif(iLF = 0, TextArea1.Text.Len, iLF - 1) - iStartPos
    TextArea1.Select(iStartPos, iLength)  ' select startpos and line length
  Endif

End
Wishing well
Bruce

If at first you don't succeed , try it differently.

bazzvn
Posts: 12
Joined: Wednesday 22nd February 2017 11:06am
Location: Vietnam

Re: TextArea - Selecting text with code [SOLVED]

Post by bazzvn » Sunday 18th July 2021 2:17am

Thank you Bruce - that does exactly what I want, and when combined with cut, copy and paste as in Cogiers code above, the selection can moved or copied to wherever I choose.
Much appreciated.
bazzvn

Post Reply