Gui: Cursor at end of Edit control Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
koenboots
Posts: 22
Joined: 17 Aug 2016, 03:13

Gui: Cursor at end of Edit control

02 Sep 2016, 03:26

Hi,

I have a button that adds text to an Edit control. I'd like the cursor to be placed at the end of the control's content to allow the user to continue typing in it right away.
Right now I'm navigating back to the control entering tab-keys, then entering a lot of down-keys and End. Like this:
TXTos:
GuiControlGet, Overig ;the Edit control is called Overig
Overig .= "`n--Besturingssysteem: "
GuiControl, Text, Overig, %Overig%
SendInput, {TAB 11}{DOWN 5}{END}
Return
There must be a better way though. I'm not sure what I can incorporate into it to achieve this in a more simple and reliable way.
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: Gui: Cursor at end of Edit control  Topic is solved

02 Sep 2016, 05:44

Try this:

Code: Select all

Gui, Add, Edit, vOverig w300 r12 hwndHandle
Gui, Add, Button, gTXTos, Button
Gui, Show
Return

TXTos:
    GuiControlGet, Overig ; the Edit control is called Overig
    Overig .= "`n--Besturingssysteem: "
    GuiControl, Text, Overig, %Overig%
    GuiControl, Focus, Overig
    SendMessage, 0xB1, -2, -1,, ahk_id %Handle%
    SendMessage, 0xB7,,,, ahk_id %Handle%

Return
I hope that helps.
garry
Posts: 1869
Joined: 22 Dec 2013, 12:50

Re: Gui: Cursor at end of Edit control

02 Sep 2016, 06:12

thank you Wolf_II
just another example

Code: Select all

Filename1=TEST1
Gui,2:Color, ControlColor,Black           ;- background from edit
Gui,2:Color, Gray                         ;- Gui color
GUI,2:Font,s12 cYellow,Lucida Console
Gui,2:Add,Edit   , x10    y10  w400 h200 vOverig +hscroll +vscroll -wrap ,----Test
Gui,2:show,,%filename1%
gosub,txtos
return
2Guiclose:
exitapp

TXTos:
Gui,2:submit,nohide
aac = %overig%`n--Besturingssysteem:
GuiControl,2:,Edit1,%aac%
ControlSend,Edit1,^{END},%Filename1%
Return

koenboots
Posts: 22
Joined: 17 Aug 2016, 03:13

Re: Gui: Cursor at end of Edit control

02 Sep 2016, 07:20

wolf_II wrote:Try this:

Code: Select all

Gui, Add, Edit, vOverig w300 r12 hwndHandle
Gui, Add, Button, gTXTos, Button
Gui, Show
Return

TXTos:
    GuiControlGet, Overig ; the Edit control is called Overig
    Overig .= "`n--Besturingssysteem: "
    GuiControl, Text, Overig, %Overig%
    GuiControl, Focus, Overig
    SendMessage, 0xB1, -2, -1,, ahk_id %Handle%
    SendMessage, 0xB7,,,, ahk_id %Handle%

Return
I hope that helps.
This is what I was hoping for actually, thanks a lot (again)!
I'm not sure what it does exactly, but it works well. I'll read into hwnd and SendMessage

Garry; thanks for your reply also. I'm going for Wolf_II's solution though. I'm not sure I completely understand your go at this either, but I might evaluate it later on for learning purposes.
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: Gui: Cursor at end of Edit control

02 Sep 2016, 07:52

What the code does, line by line:

start with your code, and remove the SendInput, {TAB 11}{DOWN 5}{END}
-> every time you hit the button, text gets appended and you loose the focus from EditBox.

put in GuiControl, Focus, Overig
-> when you hit the button, Editbox "keeps" the focus, but everything is selected

put in SendMessage, 0xB1, 5, 10,, ahk_id %Handle%
-> EM_SETSEL := 0x00B1 this allows to set the selection, for now character 5 .. 10
now play with the numbers, 0 = first character, 1 = second character, ..., -1 = last character
I found -2 to useful for putting the caret at the end, but with an empty selection.
Beware: to use -2 for StartPosition is undocumented, or it is documented and I could not find it.
see EM_SETSEL

put in SendMessage, 0xB7,,,, ahk_id %Handle%
-> scrolls the caret into view. EM_SCROLLCARET := 0x00B7
see EM_SCROLLCARET
garry
Posts: 1869
Joined: 22 Dec 2013, 12:50

Re: Gui: Cursor at end of Edit control

02 Sep 2016, 11:18

thank you wolf_II

I woul'd like to make an editor with ahk
ini-file saves last used filename and cursor-position

- when start opens last text-file and goto last cursor position
- search ( control+F and F3 search continue )
- replace ( control+H old/new and then dialog yes/all/no/cancel )
- save / save as

here a script , GOTO-LINE-x

Code: Select all

;- script GOTOLINE-X ------------------------------------
SplitPath,a_scriptname, name, dir, ext, name_no_ext, drive
filename1=%name_no_ext%
loop,12000
  {
  aa:=% SubStr("0000" A_Index, -4)
  ac .= "Line-" aa " This is Line-" aa " COL-01`; This is Line-" aa " COL-02`; This is Line-" aa " COL-03`r`n"
  }
;-------------------------------------------------
Gui,2:Color, ControlColor, Black
Gui,2:Font,S15 cYellow,Terminal
GUI,2:show,        x20  y10   w420  h260 , %filename1%
Gui,2:Add, Edit,   x5   y5    w410  h205 vEdit1a hwndEditx1 hscroll  -wrap  +0x100,
Gui,2:Add, Edit,   x120 y225  w100  h25  vGL right,11510
Gui,2:Add, Text,   x5   y227  w110 , GOTO-LINE >
Gui,2:Add, Button, x120 y225  w0    h0  gA2 default,
;--------------------------------------------------
gosub,a1
gosub,a2
return

2Guiclose:
exitapp

a1:
Gui,2:submit,nohide
GuiControl,2:,edit1a,%ac%
ac=
return

a2:
Gui,2:submit,nohide
SendMessage  0xB7,  0, 0  ,, ahk_id %Editx1%     ;- EM_SCROLLCARET
SendMessage, 0xB6 , 0, GL ,, ahk_id %Editx1%     ;- EM_LineScroll
SendMessage, 0x115, 0, 0  ,, ahk_id %Editx1%     ;- WM_VSCROLL
Guicontrol,2:focus,GL
return
;==========  END script GOTOLINEx================================================
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: Gui: Cursor at end of Edit control

02 Sep 2016, 12:57

Hi garry,

Try this for Goto line function:

Code: Select all

#SingleInstance, Force
#NoEnv

    AppName := "AHK Editor"
    Filename := A_ScriptFullPath

    Menu, SearchMenu, Add, Goto ...`tCtrl+G, SearchMenu_Goto
    Menu, MainMenu, Add, Search, :SearchMenu

    Gui, Main: New, +Resize +MinSize +LastFound
    Gui, Margin, 0, 0
    Gui, Menu, MainMenu
    Gui, Font,, Courier New
    Gui, Add, Edit, w500 r15 vEDT_File hwndHandle -Wrap
    SendMessage, 0xD3, 0x1, 8,, ahk_id %Handle%
    Gui, Font
    Gui, Add, StatusBar
    SB_SetParts(110, 60)
    SB_SetText(AppName, 1)

    Gui, Goto: New, +OwnerMain -MinimizeBox, Goto
    Gui, Add, Text, ym+4, Goto line number:
    Gui, Add, Edit, x+m ym vGotoLineNumber Right
    Gui, Add, Button, xm Default, OK

    Gui, Main: Default
    Gui, Show,, %AppName% - %Filename%

    FileRead, Text, %Filename%
    GuiControl,, EDT_File, %Text%
    ControlGetPos,,,, SB_Height, msctls_statusbar321
    SetTimer, Monitor_Caret, 50

Return

MainGuiEscape:
MainGuiClose:
ExitApp

MainGuiSize:
    GuiControl, Move, EDT_File, % "w" A_GuiWidth " h" A_GuiHeight - SB_Height
Return

Monitor_Caret:
    If WinActive(AppName " ahk_class AutoHotkeyGUI") {
        Gui, Main: Default
        ControlGet, current_Line, CurrentLine,,, ahk_id %Handle%
        ControlGet, current_Col, CurrentCol,,, ahk_id %Handle%
        If (current_Line != old_Line)
            SB_SetText("Line: " old_Line := current_Line, 2)
        If (current_Col != old_Col)
            SB_SetText("Column: " old_Col := current_Col, 3)
    }
Return

SearchMenu_Goto:
    Gui, Main: +Disabled
    Gui, Goto: Show
Return

GotoGuiEscape:
GotoGuiClose:
    Gui, Main: -Disabled
    Gui, Goto: Hide
Return

GotoButtonOK:
    Gui, Main: -Disabled
    Gui, Goto: Submit

    s := getIndex_FirstChar(GotoLineNumber)
    SendMessage, 0xB1, s, s,, ahk_id %Handle%   ; EM_SETSEL
    SendMessage, 0xB7,,,, ahk_id %Handle%       ; EM_SCROLLCARET

Return



;-------------------------------------------------------------------------------
getIndex_FirstChar(n) { ; return the index of the first character in line n
;-------------------------------------------------------------------------------
    global Text

    index := 0
    Loop, Parse, Text, `n, `r
    {
        If (A_Index = n)
            Return, index
        index += StrLen(A_LoopField) + 2
    }
}
I hope that helps.
garry
Posts: 1869
Joined: 22 Dec 2013, 12:50

Re: Gui: Cursor at end of Edit control

02 Sep 2016, 13:55

@wolf_II , thank you very much
very nice script , with goto line and see also cursor-position ( line / column )
maybe if you have already an editor script ... missing at least replace function
( but I'll be glad if you have this with all the functions below )
I woul'd like to make an editor with ahk
ini-file saves last used filename and cursor-position

- when start, opens last text-file and goto last cursor position ;- solved
- search ( control+F and F3 search continue ) ;- solved
- replace ( control+H old/new and then dialog yes/all/no/cancel ) ;- <<<< missing yet
- save / save as ;- solved
wolf_II
Posts: 2685
Joined: 08 Feb 2015, 20:55

Re: Gui: Cursor at end of Edit control

02 Sep 2016, 17:48

Hi garry,

Thank you for your interest. I don't have an editor script ready and tested, but here is a raw outline of a homemade Find and Replace in an "AHK Editor":
Not heavily tested yet !!!

Code: Select all

#SingleInstance, Force
#NoEnv

    AppName := "AHK Editor"
    Filename := A_ScriptFullPath
    CaretPos := 1


;-------------------------------------------------------------------------------
; Menu
;-------------------------------------------------------------------------------
    Menu, SearchMenu, Add, Find ...   `tCtrl+F, SearchMenu_Find
    Menu, SearchMenu, Add, Find next  `tF3,     SearchMenu_FindNext
    Menu, SearchMenu, Add, Replace ...`tCtrl+H, SearchMenu_Replace
    Menu, SearchMenu, Add ; separator
    Menu, SearchMenu, Add, Goto ...   `tCtrl+G, SearchMenu_Goto

    Menu, SearchMenu, Icon, Find ...   `tCtrl+F, shell32.dll, 23
    Menu, SearchMenu, Icon, Find next  `tF3,     shell32.dll, 23
    Menu, SearchMenu, Icon, Replace ...`tCtrl+H, shell32.dll, 239
    Menu, SearchMenu, Icon, Goto ...   `tCtrl+G, shell32.dll, 177

    Menu, MainMenu, Add, Search, :SearchMenu


;-------------------------------------------------------------------------------
Gui, Main: New, +Resize +MinSize +LastFound
;-------------------------------------------------------------------------------
    Gui, Margin, 0, 0
    Gui, Menu, MainMenu
    Gui, Font,, Courier New
    Gui, Add, Edit, w800 r25 vEDT_File hwndHandle -Wrap 0x100
    SendMessage, 0xD3, 0x1, 8,, ahk_id %Handle%
    Gui, Font
    Gui, Add, StatusBar
    SB_SetParts(110, 60)
    SB_SetText(AppName, 1)


;-------------------------------------------------------------------------------
Gui, Goto: New, +OwnerMain -MinimizeBox, Goto
;-------------------------------------------------------------------------------
    Gui, Add, Text, ym+4 w100, Goto line number:
    Gui, Add, Edit, x+m ym vGotoLineNumber Right
    Gui, Add, Button, x160 w80 Default, OK


;-------------------------------------------------------------------------------
Gui, Find: New, +OwnerMain -MinimizeBox
;-------------------------------------------------------------------------------
    Gui, Add, Tab2, w280 h150 vTabControl gSelectTab Section, Find|Replace

    ;-----------------------------------
    Gui, Tab, Find
    ;-----------------------------------
    Gui, Add, Text, xs+10 ys+34 w100, Find what:
    Gui, Add, Edit, x+m yp-4 w150 vNeedle_F
    Gui, Add, Checkbox, xs+10 y100 vMatchCase_F, Match &case
    Gui, Add, Button, xs+10 y+10 w80 gFindNext_F Default, &Find next
    Gui, Add, Button, x+m w80, &Cancel

    ;-----------------------------------
    Gui, Tab, Replace
    ;-----------------------------------
    Gui, Add, Text, xs+10 ys+34 w100, Find what:
    Gui, Add, Edit, x+m yp-4 w150 vNeedle_R
    Gui, Add, Text, xs+10 y+10 w100, Replace with:
    Gui, Add, Edit, x+m yp-4 w150 vReplacement
    Gui, Add, Checkbox, xs+10 y100 vMatchCase_R, Match &case
    Gui, Add, Button, xs+10 y+10 w80 gFindNext_R, &Find next
    Gui, Add, Button, x+m w80, &Cancel
    Gui, Add, Button, x+m w80, &Replace



;-------------------------------------------------------------------------------
; show
;-------------------------------------------------------------------------------
    Gui, Main: Default
    Gui, Show,, %AppName% - %Filename%

    FileRead, Text, %Filename%
    GuiControl,, EDT_File, %Text%
    ControlGetPos,,,, SB_Height, msctls_statusbar321
    SetTimer, Monitor_Caret, 50

Return


MainGuiEscape:
MainGuiClose:
ExitApp


MainGuiSize:
    GuiControl, Move, EDT_File, % "w" A_GuiWidth " h" A_GuiHeight - SB_Height
Return


;-------------------------------------------------------------------------------
Monitor_Caret:
;-------------------------------------------------------------------------------
    If WinActive(AppName " ahk_class AutoHotkeyGUI") {
        Gui, Main: Default
        ControlGet, current_Line, CurrentLine,,, ahk_id %Handle%
        ControlGet, current_Col, CurrentCol,,, ahk_id %Handle%
        If (current_Line != old_Line)
            SB_SetText("Line: " old_Line := current_Line, 2)
        If (current_Col != old_Col)
            SB_SetText("Column: " old_Col := current_Col, 3)
    }
Return


SearchMenu_Goto:
    Gui, Main: +Disabled
    Gui, Goto: Show
Return


GotoGuiEscape:
GotoGuiClose:
    Gui, Main: -Disabled
    Gui, Goto: Hide
Return


GotoButtonOK:
    Gui, Main: -Disabled
    Gui, Goto: Submit
    i := getIndex_FirstChar(GotoLineNumber)
    SendMessage, 0xB1, i, i,, ahk_id %Handle%
    SendMessage, 0xB7,,,, ahk_id %Handle%
Return


;-------------------------------------------------------------------------------
getIndex_FirstChar(n) { ; return the index of the first character in line n
;-------------------------------------------------------------------------------
    global Text

    index := 0
    Loop, Parse, Text, `n, `r
    {
        If (A_Index = n)
            Return, index
        index += StrLen(A_LoopField) + 2
    }
}


SelectTab:
    GuiControlGet, TabControl
    Gui, Show,, %AppName% - %TabControl%
Return


SearchMenu_Find:
    Gui, Find: Default
    GuiControl, Choose, TabControl, 1
    Gui, Show,, %AppName% - Find
Return


FindButtonCancel:
FindGuiEscape:
FindGuiClose:
    Gui, Find: Hide
    Gui, Main: Show
Return


FindNext_F:
    Gui, Submit, NoHide
    GuiControlGet, Text, Main:, EDT_File
    StringReplace, Text, Text, `n, `r`n, All ; have to deal with EOL
    l := StrLen(Needle_F)

    SearchMenu_FindNext: ; side entrance
    p := 0
    p := RegExMatch(Text, (MatchCase_F ? "" : "i)") Needle_F,, CaretPos) - 1
    IfEqual, p, -1, Return ; Needle not found
    CaretPos := p + l ; remember this between calls
    SendMessage, 0xB1, p, CaretPos,, ahk_id %Handle%
    SendMessage, 0xB7,,,, ahk_id %Handle%
Return


SearchMenu_Replace:
    Gui, Find: Default
    GuiControl, Choose, TabControl, 2
    Gui, Show,, %AppName% - Replace
Return


FindNext_R:
    Gui, Submit, NoHide
    GuiControlGet, Text, Main:, EDT_File
    StringReplace, Text, Text, `n, `r`n, All ; have to deal with EOL
    l := StrLen(Needle_R)
    p := 0
    p := RegExMatch(Text, (MatchCase_R ? "" : "i)") Needle_R,, CaretPos) - 1
    IfEqual, p, -1, Return ; Needle not found
    CaretPos := p + l ; remember this between calls
    SendMessage, 0xB1, p, CaretPos,, ahk_id %Handle%
    SendMessage, 0xB7,,,, ahk_id %Handle%
Return


FindButtonReplace:
    GuiControlGet, Replacement
    SendMessage, 0xC2, 1, &Replacement,, ahk_id %Handle%
    Gosub, FindNext_R
Return
garry
Posts: 1869
Joined: 22 Dec 2013, 12:50

Re: Gui: Cursor at end of Edit control

03 Sep 2016, 02:41

Thank you wolf_II for the ahk-editor
I would never be able to write this , works fine

Try to add follows :
when start, opens last used/saved text-file and goto last cursor position
-add an ini-file , save file-name and cursor position ( I can see the position ( Line / Column ) from cursor in your script )
-Save / Save as ...

EDIT :
it works now , I'll present the script when really finished ...

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], flyingDman, Google [Bot], grimboto, MIRKOSOFT, Urza, Wigi and 138 guests