|
Post by tsh73 on Feb 19, 2022 16:14:35 GMT -5
Could we get texteditor current position? There is
But I don't see how am I to get current position.
|
|
|
Post by Walt Decker on Feb 21, 2022 10:22:25 GMT -5
You can in a multi-line TEXTBOX but not in a TEXTEDITOR.
In a multi-line TEXTBOX get the caret pos, translate the position returned into character cell position, then use User32.dll sendmessagea() with the handle of the textbox and argument EM_CHARFROMPOS.
You will have to know the font handle in order to calculate the cell sizes returned from gdi32.dll function gettextextent32() after obtaining a dc for the text box.
|
|
bplus
Full Member
Posts: 127
|
Post by bplus on Feb 21, 2022 13:12:41 GMT -5
I recall trying or doing something with selected text ie getting the location of it in the whole text with Instr or something, years ago in JB.
It was a hack of course, not very satisfying sol'n.
|
|
|
Post by Brandon Parker on Feb 21, 2022 21:28:57 GMT -5
I know this is really convoluted, but check this out and see if it will work for you. If there is some text that is selected, the start position of that text will be returned.
There may be some strangeness if the TextEditor is empty or if the user selects beyond the text that they have entered; LB seems to have some penchant for having something at the end of the text which does not do anything...
NoMainWin
WindowWidth = 400 WindowHeight = 380
Button #Main.btnGetPosition, "Get Position", getPosition, UL, 295, 290, 85, 25 TextEditor #Main.txtEd, 5, 5, 375, 282 Open "TextEditor Position & Line Number" For Window As #Main #Main "TrapClose quit" #Main.txtEd "!CLS" #Main.txtEd "ABCDEFGHIJKLMNOPQRSTUVWXYZ" Wait
Sub getPosition handle$ #Main.txtEd "!Contents? initialContents$" If (initialContents$ <> "") Then #Main.txtEd "!Selection? initialSelection$" marker$ = initialSelection$;chr$(0);chr$(0) #Main.txtEd "!Insert marker$" #Main.txtEd "!FindBack marker$" #Main.txtEd "!Selection? selected$" #Main.txtEd "!Contents? contents$" position = Instr(contents$, selected$) #Main.txtEd "!Cut" If (initialSelection$ <> "") Then #Main.txtEd "!Insert initialSelection$" End If lineNumber = (CountSubstring(Left$(initialContents$, (position - 1)), chr$(13), 1, 0) + 1) Notice "Notice!";chr$(13);"Caret is at ";position;chr$(13);"Line number is at ";lineNumber End Sub
Sub quit handle$ Close #handle$ End End Sub
'_________________________________________________________________________________________________________________________________________________________ '_________________________________________________________________________________________________________________________________________________________
Function CountSubstring(ByRef string$, substring$, position, CountSubstring) position = Instr(string$, substring$, position) If position Then CountSubstring = CountSubstring(string$, substring$, (position + Len(substring$)), (CountSubstring + 1)) End Function
{:0)
Brandon Parker
|
|
bplus
Full Member
Posts: 127
|
Post by bplus on Feb 22, 2022 12:08:20 GMT -5
Works pretty good, great idea!
|
|
|
Post by Brandon Parker on Feb 22, 2022 21:48:34 GMT -5
Thanks! You could easily and obviously change it into a function as well to return the position for easier use in other code.
{:0)
Brandon Parker
|
|