Textarea get cursor position

Hi

I would like to know how to insert a text starting from cursor position and keep this possition for each net new line.
The goal is to using this feature for insert some code and keep its format.
For example.

My line here. (crlf)
(tab)           (tab)        Another line (crlf)
                             _(here my new position)

Usualy, i’ve this result.

My line here. (crlf)
(tab)           (tab)        Another line (crlf)
_(here my new position)

So after the (crlf) i would like to keep the cursor position just before: Another line

I use selStart and selText, all is right , just missing this feature.

How to do that. ?

Thanks

dim i as integer = textarea1.SelStart textarea1.seltext = "MOO" textarea1.SelStart = i

Ho well… ok I’ll test it and i’ll let you know

Thanks a lot

Not tested, but is (certainly) correct:

[code]Dim LineLen As Integer

LineLen = Len(Another_Line) // Or LenB, check)

textarea1.seltext = “MOO”
textarea1.SelStart = i + LineLen[/code]

Sorry, I misunderstood your question, I think this should sort it. It checks the number of tabs in front of the cursor until it hits the start of the new line. It then uses that number of tabs to put on the new line after the paste to bring the caret back to the same indent, is that what you were after?

[code]Dim tmp As String
Dim tabs As Integer = 0

For i As Integer = TextArea1.SelStart DownTo 1
tmp = textarea1.Text.Mid(i, 1)
if Asc(tmp)= 9 then
tabs = tabs + 1
End If
If Asc(tmp) = 13 or i = 1 Then
'found start of line
TextArea1.SelText = “MOO” + Chr(13)
for tabs = tabs DownTo 1
TextArea1.SelText = Chr(9)
Next
'We dont need to go back any further
Exit
End If
Next[/code]

It doesn’t work.
The first case retruns back the cursor to the start of the line like this:

and crlf “chr(10) & chr(13)” is ignored.
Instead to get :

[quote] MOO
_[/quote]

The second case doesn’t work too.
It retruns back to the first position to the margin

@

Yes. correct
I’ll try it and give a feedback

My fault: I do not read correctly the DIM line:

[code]Dim i as integer
Dim LineLen As Integer

LineLen = Len(Another_Line) // Or LenB, check)

textarea1.seltext = “MOO”
i = Len(textarea1.Text) // This is when you append text, not for when you insert text
textarea1.SelStart = i + LineLen[/code]

In short: I store in i the position after MOO,
Add it to the length of the text in Another_Line
And set the cursor position at the end of Another_Line.

Now if you want to set the cursor at the end of the text, use:

textarea1.SelStart = Len(textarea1.text)

More details here: TextArea .

Change

If Asc(tmp) = 13 Then

to

If Asc(tmp) = 13 or i = 1 Then

Or it wont work on the first line (as there’s no CR at the start of the textarea)

It’s better but the incovenient is , the number of tab at the end of the second line is wrong.
it gives :

[quote]tab (my line)
tab (my line) <- ok
tab tab[/quote]

In changed If Asc(tmp) = 13 or i = 1 Then

One tab too much, but it’s the idea.
Thanks anyway :slight_smile:

It’s idiot that textaera don’t provide this kind of stuff. Be able to return the cursor position !!!

Here you go, different platforms put different characters in to denote carriage returns etc. This should cover them all.

[code]Dim tmp As String
Dim tabs As Integer = 0

For i As Integer = TextArea1.SelStart DownTo 1
tmp = textarea1.Text.Mid(i, 1)
'System.DebugLog(tmp + “=” + Str(Asc(tmp)))
If Asc(tmp)= 9 Then
tabs = tabs + 1
End If
If (Asc(tmp) > 9 And Asc(tmp) < 14) Or i = 1 Then
'found start of line
TextArea1.SelText = “MOO” + Chr(13)
For tabs = tabs DownTo 1
TextArea1.SelText = Chr(9)
Next
'We dont need to go back any further
Exit
End If
Next[/code]

TextArea.SelStart is the cursor position in characters from the start of the textarea.

Sometimes. Other times it report the position of the first selected character in the TextArea.

RTFM to know better / and maybe understand better.

Edit: unless you forgot to tell things to us (this happens to me…)