Passing variables from a gui between functions

Get help with using AutoHotkey and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 863
Joined: 29 Sep 2013, 22:18

Passing variables from a gui between functions

01 May 2020, 22:41

Apologies, I feel I've asked this before but I can't find the answer. In the following code why isn't the msgbox displaying the result from the Gui eidt control?

Code: Select all

^f4:: functiontest()

functiontest()
{
	n := secondfunction()
	winwaitclose, AFN
	msgbox, 4096, OK, %n%
	exitapp
}

Secondfunction()
{
	static nam
	Gui, AFN:New, +AlwaysOnTop -caption border, AFN
	Gui, margin, 20, 20
	Gui, font, s30 normal
	Gui, add, edit, w360 h60 vnam -0x200000 -WantReturn
	Gui, add, button, w0 h0 gAFN default
	Gui, show
	
	return

	AFN:
	Gui, AFN:submit
	r :=  nam

	AFNGuiescape:
	Gui AFN:destroy
	return, r
}
If I tried something like r := "Hello World" in Secondfunction() I get Hello World in the msgbox. What am I doing wrong with the Gui? Thanks.
User avatar
boiler
Posts: 5249
Joined: 21 Dec 2014, 02:44

Re: Passing variables from a gui between functions

01 May 2020, 23:01

It's because when you call SecondFunction, you immediately return with no value after creating the GUI, so that's return value of the function, not the value of r that you return later. Replace the bare return with WinWaitClose, AFN and it works because the return r is what eventually returns the value of the function.
User avatar
PuzzledGreatly
Posts: 863
Joined: 29 Sep 2013, 22:18

Re: Passing variables from a gui between functions

02 May 2020, 00:32

Arg! It's obvious now you have explained it. Thanks, boiler.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, JackMa, rpgfreak128 and 273 guests