March 03, 2021, 02:41:12 AM

Author Topic: Manually position cursor (claret) in a TextField  (Read 124 times)

Offline chalky

  • Jr. Member
  • **
  • Posts: 75
Manually position cursor (claret) in a TextField
« on: January 17, 2021, 09:29:59 PM »
I am writing a program which passes some text to a TextField in a new window. I would like to automatically activate the TextField and move the cursor (claret) to the end of the text when the window opens. I have tried using ActivateGadget, but that causes all the text in the TextField to become selected.

I can do what I want using a TextArea, but this is no good because the text gadget needs to be 1 row of text high only, and as soon as the contents of a TextArea extend beyond its width a scrollbar is displayed, covering the text so it cannot be seen/accessed.

Is there a way of activating a TextField without selecting all its contents - and can the cursor be positioned manually (maybe via an API [Windows])?

Alternatively - is there a way to disable/hide the scrollbars of a TextArea?

Offline Henri

  • Sr. Member
  • ****
  • Posts: 294
Re: Manually position cursor (claret) in a TextField
« Reply #1 on: January 17, 2021, 10:45:18 PM »
Hi chalky,

I use something like this to position caret/cursor:

Code: [Select]
Function SetCaretPos(gadget:TGadget, pos:Int = -1) '-1 = end of text

If Not gadget Then Return

If pos = -1 Then
SelectTextAreaText(gadget, TextAreaLen(gadget), 0 )
Else
SelectTextAreaText(gadget, pos, 0 )
EndIf

EndFunction

EDIT:

Didn't notice that you meant Textfields so here is the Win code for setting the position. Notice that if the field is not active, the caret is not shown. You can do that by using ActivateGadget before setting the position.

Code: [Select]
Local hwnd:Byte Ptr = QueryGadget(textfield, QUERY_HWND)
Local start:Int = 5
Local stop:Int = 5

SendMessageW(hwnd, EM_SETSEL, WParam(start), stop) 

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Offline Henri

  • Sr. Member
  • ****
  • Posts: 294
Re: Manually position cursor (claret) in a TextField
« Reply #2 on: January 17, 2021, 10:58:15 PM »
..and alternatively how to disable vertical and horizontal scrollbars using Windows API (ng):

Code: [Select]
Local hwnd:Byte Ptr = QueryGadget(myTextarea, QUERY_HWND)

SendMessageW(hwnd, EM_SHOWSCROLLBAR, WParam(SB_VERT), Null)
SendMessageW(hwnd, EM_SHOWSCROLLBAR, WParam(SB_HORZ), Null)

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Offline chalky

  • Jr. Member
  • **
  • Posts: 75
Re: Manually position cursor (claret) in a TextField
« Reply #3 on: January 18, 2021, 10:07:20 AM »
Didn't notice that you meant Textfields so here is the Win code for setting the position. Notice that if the field is not active, the caret is not shown. You can do that by using ActivateGadget before setting the position.

Code: [Select]
Local hwnd:Byte Ptr = QueryGadget(textfield, QUERY_HWND)
Local start:Int = 5
Local stop:Int = 5

SendMessageW(hwnd, EM_SETSEL, WParam(start), stop) 

-Henri

Brilliant - thanks Henri. This method does limit my program to Windows, but will do very nicely until I can figure out how to achieve the same on Mac.  :)

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal