TextArea - Selecting text with code

Post

Posted
Rating:
#1 (In Topic #701)
Trainee
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

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
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’

bazzvn said

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

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.

Code (gambas)

  1.  iFirst = TextArea1.ToPos(iLin, 0)
  2.  
  3.   sText = "Current cursor position = " & TextArea1.Pos
  4.   sText &= gb.NewLine & "Current line = " & iLin & ";   Current column = " & iCol
  5.   sText &= gb.NewLine & "Position at start of line " & iLin & " = " & iFirst
  6.   TextArea2.Text = sText
  7.  
  8.  TextArea1.Select(iFirst, 1) ''####### THIS IS THE PROBLEM LINE
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
This may not do everything you require, but I hope it points you in the right direction.

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   For iLoop As Integer = 0 To 9
  4.     TextArea1.Text &= Str(iLoop) & ". This is line " & Str(iLoop) & gb.NewLine
  5.   Next
  6.  
  7.  
  8. Public Sub TextArea1_MouseUp()
  9.  
  10.   TextArea1.Copy
  11.   TextArea2.Text &= Clipboard.Paste() & gb.NewLine
  12.  

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

<IMG src="https://www.cogier.com/gambas/gb_button.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#4
Trainee
 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.
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
How's this….
[EDIT: handle if on last line]

Code (gambas)

  1. Public Sub TextArea1_MouseUp()
  2.  
  3.   ' Set start pos to beginning of the line
  4.   Dim iStartPos As Integer = TextArea1.ToPos(TextArea1.Line, 0)
  5.  
  6.  ' get pos of next "\n"
  7.   Dim iLF As Integer = InStr(TextArea1.Text, "\n", iStartPos + 1)
  8.  
  9. ' length is LF pos - 1 (or end of file) minus start pos
  10.   Dim iLength As Integer = Iif(iLF = 0, TextArea1.Text.Len, iLF - 1) - iStartPos
  11.  
  12.   TextArea1.Select(iStartPos, iLength)  ' select startpos and line length
  13.  
  14.  
  15.  
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
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..

Code (gambas)

  1. Public Sub TextArea1_MouseUp()
  2.  
  3.   If Mouse.Alt Then
  4.     Dim iStartPos As Integer = TextArea1.ToPos(TextArea1.Line, 0)
  5.     Dim iLF As Integer = InStr(TextArea1.Text, "\n", iStartPos + 1)
  6.     Dim iLength As Integer = Iif(iLF = 0, TextArea1.Text.Len, iLF - 1) - iStartPos
  7.     TextArea1.Select(iStartPos, iLength)  ' select startpos and line length
  8.  
  9.  
Online now: No Back to the top

Post

Posted
Rating:
#7
Trainee
 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
Online now: No Back to the top
1 guest and 0 members have just viewed this.