GUI - focus on the Edit-field

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

GUI - focus on the Edit-field

19 May 2021, 15:58

This test script have a GUI in a function(), and have two radiobuttons and one edit-field.

I want to set focus on the edit field when the function is called.
(Now I have to point on the field before anything can be written in the field)

Code: Select all

#SingleInstance force
Func()
ExitApp

Func()
{	Static Fakt1, Fakt2, inpTxt
	
	Suspend On  ; Suspends hotstrings while GUI is shown
	Gui 1: New, hwndhGui
	Gui 1: +AlwaysOnTop	; This is to keep the GUI on top, because I'm cutting and pasting from another app
	; Gui 2: +LastFound		; Sets the window to be the "Last Found Window"
	
	Gui 1: Font, cBlue s12 Bold, Verdana
	Gui 1: Add, Radio, x250 y20 vFakt1 gInputText Group checked, R-butt1
	Gui 1: Add, Radio, xp yp+25 vFakt2 gInputText, R-butt2

	; Editfield
	Gui 1: Font, cBlue s10 Normal, Verdana
	Gui 1: Add, Text, xp yp+30 w145 h20, Test editfield
	Gui 1: Font, cRed s16 Bold, Verdana
	Gui 1: Add, Edit, xp yp+25 w200 +Center vInpTxt gInputText
	; ControlFocus , Control, WinTitle, WinText, ExcludeTitle, ExcludeText	
	
	Gui 1: Show, w500 h350, Test GUI
	
	WinWaitActive ahk_id %hGui%
	WinWaitClose ahk_id %hGui%
	Suspend Off		; Re-enable hotstrings that were disable above

	Return

	InputText:
	Return
}


ESC::
	ExitApp
Return
How?
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: GUI - focus on the Edit-field

19 May 2021, 16:48

Insert the following as line #22

Code: Select all

  GuiControl, 1:Focus, InpTxt
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: GUI - focus on the Edit-field

11 Jun 2021, 15:07

The solution works in the test program (with few lines)
But on the main program it doesn't work

Code: Select all

...
		Gui 2: Add, Edit, xp+15 yp+25 w200 +Center vLogoLibName gInputName hwndhwnd
		GuiControl 2: Focus, LogoLibName
		Handles[hwnd] := {Type:"Edit"}
....
Any idea why?
User avatar
mikeyww
Posts: 26612
Joined: 09 Sep 2014, 18:38

Re: GUI - focus on the Edit-field

11 Jun 2021, 15:15

It's probably hard to say more without more code.

Worked in my test.

Code: Select all

Gui, 2:New
Gui, Add, Edit, vLogoLibName3
Gui, Add, Edit, vLogoLibName2
Gui, Show, h400
Gui, Add, Edit, +Center vLogoLibName
GuiControl 2: Focus, LogoLibName
If in a function, pay special attention to what variables are local vs. global-- may be the key difference. Functions use local variables by default.
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: GUI - focus on the Edit-field

11 Jun 2021, 18:34

Thank you!
Yes - The Gui is in a Function()
Yes - I have tried to set the LogoLibName from Static to Global - but no change.
Yes - Also this testprogram works
More complete test program
Unfortunately, this test program is difficult to test for someone who does not know all the specifications.



I found the "problem" - but not the solution.
The Gui window also displays a PDF file in this way

Code: Select all

...
xxa=Shell.Explorer ; ActiveX PDFx
...
Gui 2: Add, ActiveX, x500 yp w670 h300 vWB1, %xxa%	; Displays the PDF file in a GUI window.
WB1.Navigate(pdfInvoice.file)
...
It works, Acrobat Reader opens in a GUI window (very nice)
Would have liked another simpler PDF program had been opened.
But, I do not understand what is happening.

When this line GuiControl 2: Focus, LogoLibName is in the Code - It does not matter - the focus is somewhere else.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: GUI - focus on the Edit-field

12 Jun 2021, 03:31

Albireo wrote:
11 Jun 2021, 15:07
The solution works in the test program (with few lines)
But on the main program it doesn't work

Code: Select all

...
		Gui 2: Add, Edit, xp+15 yp+25 w200 +Center vLogoLibName gInputName hwndhwnd
		GuiControl 2: Focus, LogoLibName
		Handles[hwnd] := {Type:"Edit"}
....
Any idea why?
Maybe following would work:

Code: Select all

GuiControl, Focus, %hwnd%

Edit: I have marked this topic as unsolved
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: GUI - focus on the Edit-field

12 Jun 2021, 15:19

Thanks for the suggestion!
No, GuiControl, Focus, %hwnd% give the same result as before.

I wonder if the order of what happens first / later when Gui is created can matter.
(however, it is a bit of a job to test with relative x / y coordinates)
The PDF-file-window is created just before Gui Show (and the Gui Edit field earlier in the script)
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: GUI - focus on the Edit-field

13 Jun 2021, 06:23

Wrote a small test script that is easier to handle.
If the PDF file name, at the beginning of the script is correct, the focus will not be on the Edit field.
If PDF does not exist, the focus will be on the Edit field.

Code: Select all

#SingleInstance force

filePDF := "c:\Temp\TestPdf.pdf"
Func(filePDF)
ExitApp

Func(filePDF)
{	Static Fakt1, Fakt2, inpTxt, WB1
	
	Suspend On  ; Suspends hotstrings while GUI is shown
	Gui 1: New, hwndhGui
	Gui 1: +AlwaysOnTop	; This is to keep the GUI on top, because I'm cutting and pasting from another app
	; Gui 2: +LastFound		; Sets the window to be the "Last Found Window"
	
	Gui 1: Font, cBlue s12 Bold, Verdana
	Gui 1: Add, Radio, x250 y20 vFakt1 gInputText Group checked, R-butt1
	Gui 1: Add, Radio, xp yp+25 vFakt2 gInputText, R-butt2

	; Editfield
	Gui 1: Font, cBlue s10 Normal, Verdana
	Gui 1: Add, Text, xp yp+30 w145 h20, Test editfield
	Gui 1: Font, cRed s16 Bold, Verdana
	Gui 1: Add, Edit, xp yp+25 w200 +Center vInpTxt gInputText
	GuiControl 1: Focus, InpTxt
	
	xxa=Shell.Explorer ; ActiveX PDFx 
	Gui 1: Add, ActiveX, x500 yp w400 h300 vWB1, %xxa%	; Visar PDF-filen i ett GUI-fönster.
	WB1.Navigate(filePDF)	
		
	Gui 1: Show, h450, Test GUI
	
	WinWaitActive ahk_id %hGui%
	WinWaitClose ahk_id %hGui%
	Suspend Off		; Re-enable hotstrings that were disable above

	Return

	InputText:
	Return
}

GuiClose:
GuiContextMenu:
; *ESC::ExitApp
ESC::
	Suspend Permit
	ExitApp
Return
User avatar
Hellbent
Posts: 2103
Joined: 23 Sep 2017, 13:34

Re: GUI - focus on the Edit-field

13 Jun 2021, 06:35

Why don't you have the GuiControl, Focus after you have added all the controls?
User avatar
Hellbent
Posts: 2103
Joined: 23 Sep 2017, 13:34

Re: GUI - focus on the Edit-field

13 Jun 2021, 07:20

I don't know anything about activex controls or pdf's nor do I have any pdf's on my computer so I can't / won't run your code.

I did however change your activeX code to the code found in the ahk doc.

https://www.autohotkey.com/docs/commands/GuiControls.htm#ActiveX

Code: Select all

Gui Add, ActiveX, w980 h640 vWB1, Shell.Explorer  ; The final parameter is the name of the ActiveX component.
WB1.Navigate("https://www.autohotkey.com/boards/")  ; This is specific to the web browser control.
That code kept giving me a ton of error messages but was able to run well enough to see that the edit was getting focus so it may have something to do with using a pdf? (don't know anything about them or what they are).

I have slightly altered your code to allow a hotkey so you can see where your focus is.

Code: Select all

#SingleInstance force

filePDF := "c:\Temp\TestPdf.pdf"
Func(filePDF)
ExitApp

Func(filePDF)
{	Static Fakt1, Fakt2, inpTxt, WB1
	
	;~ Suspend On  ; Suspends hotstrings while GUI is shown
	Gui 1: New, hwndhGui
	Gui 1: +AlwaysOnTop	; This is to keep the GUI on top, because I'm cutting and pasting from another app
	; Gui 2: +LastFound		; Sets the window to be the "Last Found Window"
	
	Gui 1: Font, cBlue s12 Bold, Verdana
	Gui 1: Add, Radio, x250 y20 vFakt1 gInputText Group checked, R-butt1
	Gui 1: Add, Radio, xp yp+25 vFakt2 gInputText, R-butt2

	; Editfield
	Gui 1: Font, cBlue s10 Normal, Verdana
	Gui 1: Add, Text, xp yp+30 w145 h20, Test editfield
	Gui 1: Font, cRed s16 Bold, Verdana
	Gui 1: Add, Edit, xp yp+25 w200 +Center vInpTxt gInputText
	GuiControl 1: Focus, InpTxt
	
	xxa=Shell.Explorer ; ActiveX PDFx 
	Gui 1: Add, ActiveX, x500 yp w400 h300 vWB1, %xxa%	; Visar PDF-filen i ett GUI-fönster.
	WB1.Navigate(filePDF)	
		
	Gui 1: Show, h450, Test GUI
	
	WinWaitActive ahk_id %hGui%
	WinWaitClose ahk_id %hGui%
	Suspend Off		; Re-enable hotstrings that were disable above

	Return

	InputText:
	Return
}


*Numpad1::
	GuiControlGet, FocusedControl, 1:Focus
	ToolTip, % FocusedControl
	return


GuiClose:
GuiContextMenu:
; *ESC::ExitApp
ESC::
	Suspend Permit
	ExitApp
Return


run that and then press Numpad1.
garry
Posts: 3743
Joined: 22 Dec 2013, 12:50

Re: GUI - focus on the Edit-field

13 Jun 2021, 10:06

concerns show PDF in ActiveX , this worked before but now no more works , I see a dark gray field in ActiveX

Code: Select all

xxa=Shell.Explorer
F1:="C:\test.pdf"
stringreplace,f1,f1,\,/,all
F2:="file:///" . F1
Gui,2: Add, ActiveX,x20 y5 w980 h600 vWB,%xxa%
WB.Silent := True
WB.Navigate(F2)
Gui 2: Add, Edit, x20 y620 w200 h30 -vscroll vInpTxt
Gui,2: Show,x0 y0 w1024 h670, my gui
GuiControl,2: Focus, inptxt
return

2Guiclose:
exitapp
;=====================================
Albireo
Posts: 1747
Joined: 16 Oct 2013, 13:53

Re: GUI - focus on the Edit-field

14 Jun 2021, 05:33

garry wrote:
13 Jun 2021, 10:06
concerns show PDF in ActiveX , this worked before but now no more works , I see a dark gray field in ActiveX

Code: Select all

xxa=Shell.Explorer
F1:="C:\test.pdf"
stringreplace,f1,f1,\,/,all
F2:="file:///" . F1
Gui,2: Add, ActiveX,x20 y5 w980 h600 vWB,%xxa%
WB.Silent := True
WB.Navigate(F2)
Gui 2: Add, Edit, x20 y620 w200 h30 -vscroll vInpTxt
Gui,2: Show,x0 y0 w1024 h670, my gui
GuiControl,2: Focus, inptxt
return

2Guiclose:
exitapp
;=====================================
Interesting solution. (However, do not understand much :oops: )

1) Does it matter
To write this code .:

Code: Select all

xxa = Shell.Explorer
...
Gui, 2: Add, ActiveX, x20 y5 w980 h600 vWB, %xxa%
instead of

Code: Select all

Gui, 2: Add, ActiveX, x20 y5 w980 h600 vWB, Shell.Explorer
2) Explanation of the instruction below
What do these program lines do?
(Does this mean that the acrobat reader starts without any questions? - which I would like)

Code: Select all

WB.Silent := True
WB.Navigate(F2)
3) The main problem seems to be on which order the GUI is written.
Changed the order so that the PDF file appears first in the GUI definition and the edit field last,
and then the focus seems to end up right in the edit field. (Have not made changes and tested the main program yet)

Code: Select all

#SingleInstance force

filePDF := "c:\Temp\TestPdf.pdf"
Func(filePDF)
ExitApp

Func(filePDF)
{	Static Fakt1, Fakt2, inpTxt, WB1
	
	;~ Suspend On  ; Suspends hotstrings while GUI is shown
	Gui 1: New, hwndhGui
	Gui 1: +AlwaysOnTop	; This is to keep the GUI on top, because I'm cutting and pasting from another app
	; Gui 2: +LastFound		; Sets the window to be the "Last Found Window"

	Gui 1: Add, ActiveX, x500 y100 w400 h300 vWB1, Shell.Explorer
	WB1.Navigate(filePDF)
		
	Gui 1: Font, cBlue s12 Bold, Verdana
	Gui 1: Add, Radio, x250 y20 vFakt1 gInputText Group checked, R-butt1
	Gui 1: Add, Radio, xp yp+25 vFakt2 gInputText, R-butt2

	
	; Editfield
	Gui 1: Font, cBlue s10 Normal, Verdana
	Gui 1: Add, Text, xp yp+30 w145 h20, Test editfield
	Gui 1: Font, cRed s16 Bold, Verdana
	Gui 1: Add, Edit, xp yp+25 w200 +Center vInpTxt gInputText
	GuiControl 1: Focus, InpTxt	
	
	
	Gui 1: Show, h450, Test GUI
	
	WinWaitActive ahk_id %hGui%
	WinWaitClose ahk_id %hGui%
	Suspend Off		; Re-enable hotstrings that were disable above
	
	Return

	InputText:
	Return
}


GuiClose:
GuiContextMenu:
; *ESC::ExitApp
ESC::
	Suspend Permit
	ExitApp
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, mcd, mikeyww, Ocelot and 121 guests