Page 1 of 1

TextArea - Selecting text with code

Posted: Wednesday 14th July 2021 9:43am
by bazzvn
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

Re: TextArea - Selecting text with code

Posted: Wednesday 14th July 2021 9:55am
by BruceSteers
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

Re: TextArea - Selecting text with code

Posted: Wednesday 14th July 2021 2:02pm
by cogier
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

Re: TextArea - Selecting text with code

Posted: Wednesday 14th July 2021 3:36pm
by bazzvn
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.

Re: TextArea - Selecting text with code

Posted: Wednesday 14th July 2021 11:39pm
by BruceSteers
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


Re: TextArea - Selecting text with code

Posted: Thursday 15th July 2021 12:13am
by BruceSteers
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

Re: TextArea - Selecting text with code [SOLVED]

Posted: Sunday 18th July 2021 2:17am
by bazzvn
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