Passing GUI entry to function Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Decar
Posts: 2
Joined: 15 Apr 2024, 16:06

Passing GUI entry to function

15 Apr 2024, 16:29

I need help to get my GUI to pass information from an input box. I spent a few hours trying to make an edit input field pass information to use downstream. I finally found an example that works, but only if I call the GUI directly when starting the script up. I get errors when I initialize the GUI with a button. I'm assuming this has something to do with Global variables, but declaring "MyGui := Gui()" in the second type still shows an error. The error says "Error: This value of type "Object" has no property named "FirstName"." I cannot find anything that helped me understand or solve it, including ChatGPT.


Example Code that works

Code: Select all

MyGui := Gui()
MyGui.Add("Edit", "vFirstName")
MyGui.Add("Button", "default", "OK").OnEvent("Click", ProcessUserInput)
MyGui.Show()

ProcessUserInput(*)
{
	MsgBox("You entered '" MyGui.Submit().FirstName)
}


My code
This does not work and throws errors. I added the "MyGui := Gui()" to try and declare the GUI as a global object/variable, but then it says "Error: This value of type "Object" has no property named "FirstName"."

Code: Select all

MyGui := Gui()

!Numpad1::
{
MyGui := Gui()
MyGui.Add("Edit", "vFirstName")
MyGui.Add("Button", "default", "OK").OnEvent("Click", ProcessUserInput)
MyGui.Show()
}

ProcessUserInput(*)
{
	MsgBox("You entered '" MyGui.Submit().FirstName)
}
User avatar
boiler
Posts: 16996
Joined: 21 Dec 2014, 02:44

Re: Passing GUI entry to function  Topic is solved

15 Apr 2024, 18:48

Remove the MyGui := Gui() from inside the function. For one thing, it doesn’t make sense to create the object twice. The other issue is you don’t want to declare it locally. Just leave the global declaration so the other function can see it.

By the way, if you plan on using the hotkey more than once, you don’t have it set up to do that. You would want to create the controls once and just use the hotkey to display the GUI.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: alawsareps, Atom, ManuelesAdrian and 89 guests