How to access variables of Gui callback event in AHK v2? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
yousef_badr23
Posts: 19
Joined: 26 Aug 2022, 07:18

How to access variables of Gui callback event in AHK v2?

03 Mar 2023, 10:11

I am trying to get the content of a edit control in a Gui i create in AHK v2, triggered after pressing a submit button, as the following code:

Code: Select all

myGui:=Gui()
myGui.addEdit("vInput")
myGui.addButton(,"Submit").onEvent("Click",(*)=> output:=myGui.submit())
myGui.show()
winwaitclose myGui
msgbox output.Input
The problem is that I can't access the output variable. I think because the callback function is its own thread, and that it does not return the variable to the script. Is there a proper way to submit Gui in AHK v2? How to use the callback event to return variables to the main script/ assign values to variable??
teadrinker
Posts: 4389
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to access variables of Gui callback event in AHK v2?  Topic is solved

03 Mar 2023, 10:41

As an option:

Code: Select all

myGui:=Gui()
myGui.addEdit("vInput")
myGui.addButton(,"Submit").onEvent("Click",(*)=> (myGui.output := myGui.submit()))
myGui.show()
winwaitclose myGui
msgbox myGui.output.Input
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: How to access variables of Gui callback event in AHK v2?

03 Mar 2023, 10:44

There are numerous ways to do this. One would be:

Code: Select all

myGui:=Gui()
myGui.addEdit("vInput")
myGui.addButton(,"Submit").onEvent("Click", submit_handler)
myGui.show()

submit_handler(ctrl, *) {
    output := ctrl.gui.submit()
    msgbox(output.Input)
}
yousef_badr23
Posts: 19
Joined: 26 Aug 2022, 07:18

Re: How to access variables of Gui callback event in AHK v2?

03 Mar 2023, 10:55

kczx3 wrote:
03 Mar 2023, 10:44
There are numerous ways to do this. One would be:

Code: Select all

myGui:=Gui()
myGui.addEdit("vInput")
myGui.addButton(,"Submit").onEvent("Click", submit_handler)
myGui.show()

submit_handler(ctrl, *) {
    output := ctrl.gui.submit()
    msgbox(output.Input)
}
I want to use the variable outsite the callback function
User avatar
flyingDman
Posts: 2838
Joined: 29 Sep 2013, 19:01

Re: How to access variables of Gui callback event in AHK v2?

03 Mar 2023, 11:05

Code: Select all

myGui:=Gui()
edt1 := myGui.addEdit("vInput")
myGui.addButton(,"Submit").onEvent("Click",(*)=> myGui.submit())
myGui.show
winwaitclose myGui
msgbox edt1.text
You do not even need vInput.
14.3 & 1.3.7
teadrinker
Posts: 4389
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to access variables of Gui callback event in AHK v2?

03 Mar 2023, 11:16

flyingDman wrote: You do not even need vInput.
Moreover, you don't need submit() in this case. :)

Code: Select all

myGui:=Gui()
editObj := myGui.addEdit()
myGui.addButton(,"Submit").onEvent("Click",(*)=> myGui.Hide())
myGui.show()
winwaitclose myGui
msgbox editObj.Text
yousef_badr23
Posts: 19
Joined: 26 Aug 2022, 07:18

Re: How to access variables of Gui callback event in AHK v2?

03 Mar 2023, 11:25

teadrinker wrote:
03 Mar 2023, 11:16
flyingDman wrote: You do not even need vInput.
Moreover, you don't need submit() in this case. :)

Code: Select all

myGui:=Gui()
editObj := myGui.addEdit()
myGui.addButton(,"Submit").onEvent("Click",(*)=> myGui.Hide())
myGui.show()
winwaitclose myGui
msgbox editObj.Text
The problem with this approach is that I can't close the gui to stop the script, it will always get the control text.
teadrinker
Posts: 4389
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to access variables of Gui callback event in AHK v2?

03 Mar 2023, 12:51

yousef_badr23 wrote: The problem with this approach is that I can't close the gui to stop the script, it will always get the control text.
Why not:

Code: Select all

myGui:=Gui()
myGui.onEvent('Close', (*) => ExitApp())
editObj := myGui.addEdit()
myGui.addButton(,"Submit").onEvent("Click",(*)=> myGui.Hide())
myGui.show()
winwaitclose myGui
msgbox editObj.Text
yousef_badr23
Posts: 19
Joined: 26 Aug 2022, 07:18

Re: How to access variables of Gui callback event in AHK v2?

03 Mar 2023, 13:00

teadrinker wrote:
03 Mar 2023, 12:51
yousef_badr23 wrote: The problem with this approach is that I can't close the gui to stop the script, it will always get the control text.
Why not:

Code: Select all

myGui:=Gui()
myGui.onEvent('Close', (*) => ExitApp())
editObj := myGui.addEdit()
myGui.addButton(,"Submit").onEvent("Click",(*)=> myGui.Hide())
myGui.show()
winwaitclose myGui
msgbox editObj.Text
Better! Is there a way to use Exit instead of ExitApp? I understand that having a separate thread for the event would make this difficult.
teadrinker
Posts: 4389
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to access variables of Gui callback event in AHK v2?

03 Mar 2023, 13:42

Maybe like this:

Code: Select all

myGui:=Gui()
myGui.onEvent('Close', (*) => myGui.Destroy())
editObj := myGui.addEdit()
myGui.addButton(,"Submit").onEvent("Click",(*)=> myGui.Hide())
myGui.show()
winwaitclose myGui
if !WinExist()
    return
msgbox editObj.Text
yousef_badr23
Posts: 19
Joined: 26 Aug 2022, 07:18

Re: How to access variables of Gui callback event in AHK v2?

03 Mar 2023, 16:39

teadrinker wrote:
03 Mar 2023, 13:42
Maybe like this:

Code: Select all

myGui:=Gui()
myGui.onEvent('Close', (*) => myGui.Destroy())
editObj := myGui.addEdit()
myGui.addButton(,"Submit").onEvent("Click",(*)=> myGui.Hide())
myGui.show()
winwaitclose myGui
if !WinExist()
    return
msgbox editObj.Text
Thanks alot!!!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: shipaddicted and 22 guests