TextArea - Selecting text with code
Posted
#1
(In Topic #701)
Trainee
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
Posted
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)
- 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
Posted
Guru

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>
Posted
Trainee
Posted
Guru

[EDIT: handle if on last line]
Code (gambas)
- ' Set start pos to beginning of the line
- ' get pos of next "\n"
- ' length is LF pos - 1 (or end of file) minus start pos
Posted
Guru

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)
Posted
Trainee
Much appreciated.
bazzvn
1 guest and 0 members have just viewed this.



