Page 1 of 1

Getting the position of the cursor in a text field.

Posted: Thu Oct 15, 2009 3:45 pm
by andyh1234
Sorry to ask, but im hitting a little brick wall with this one!

Is it possible to find the location of the cursor in a text box?

I just need it so I can return a user to the same cursor position after Ive run some other code that is causing the text box to lose its focus. When I focus back on the control it places the cursor at the end and not where it was, which is a little annoying for the user.

Id guess I need to use....

select after char tMyChar of field "myField"

to place the user where I want them, but how do I find out the value of tMyChar.

If its not possible, Ill try to figure out how to keep the focus on the control while executing the code, but the quick option for me at the moment would to simply log the position in a temporary variable, then pick it back up when the code is finished.

Thanks

Andy

Posted: Thu Oct 15, 2009 4:07 pm
by andyh1234
Sorry, managed to get there in the end.

Just posting the solution I found in case its any use to anyone else...

Code: Select all

set the itemdel to space
put item 4 of the selectedchunk of field "myField" into tMyCharLoc

-- Code here that loses the focus...,  then to re-gain it...

select after char tMyCharLoc of field "myField"
Im sure there are more elegant ways to do it, but this seems to work!

Thanks

Andy

Posted: Thu Oct 15, 2009 8:00 pm
by Janschenkel
You don't have to change the itemDelimiter - just use the word chunk

Code: Select all

put word 2 of the selectedChunk of field "Foobar" into tSelectionStart
put word 4 of the selectedChunk of field "Foobar" into tSelectionEnd
if tSelectionEnd < tSelectionStart then
  -- the cursor is currently between two chars
else
  -- there is actually a chunk of text selected
end if
HTH,

Jan Schenkel.

Posted: Thu Oct 15, 2009 10:10 pm
by andyh1234
Thanks Jan

Re: Getting the position of the cursor in a text field.

Posted: Sat Jan 29, 2022 5:47 pm
by richmond62
Insert.png