Manually position cursor (claret) in a TextField

Started by chalky, January 17, 2021, 21:29:59

Previous topic - Next topic

chalky

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?

Henri

#1
Hi chalky,

I use something like this to position caret/cursor:


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.


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

Henri

..and alternatively how to disable vertical and horizontal scrollbars using Windows API (ng):


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

chalky

#3
Quote from: Henri on January 17, 2021, 22:45:18
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.


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.  :)