How to deal with functions with parameters in the OnEvent method Topic is solved

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
OswaldGalen
Posts: 2
Joined: 05 Aug 2020, 20:02

How to deal with functions with parameters in the OnEvent method

05 Aug 2020, 21:02

I'm new to AutoHotKey and want to build a program that launches certain apps through a GUI.
I'm stuck on the OnEvent method in a loop. A part of my script is as follows:

Code: Select all

loop read, "button.txt"
{
    Name := (StrSplit(A_LoopReadLine, ","))[1]
    Posit := (StrSplit(A_LoopReadLine, ","))[2]
    Entry.Add("Button", Posit, Name).OnEvent("Click", (*) => Handle(Name))
}
The problem is that every button is linked to the function Handle with the same parameter value, namely the value of variable Name in the last iteration.
I thought it is because the variable Name is a string and thus is only referenced. But it is the same when variable Name is a just a number.
I guess the parameters of the function Handle are all stored as a reference.
Can anyone provide a suggestion on this?
I'm not a native English speaker, so if there is anything inappropriate in the language, please forgive me.

Thanks
Oswald
swagfag
Posts: 4135
Joined: 11 Jan 2017, 17:59

Re: How to deal with functions with parameters in the OnEvent method  Topic is solved

05 Aug 2020, 23:49

u guess right. see https://www.autohotkey.com/boards/viewtopic.php?f=82&t=79132, https://www.autohotkey.com/boards/viewtopic.php?f=14&t=70515
the fix is binding the value to the callback, though in ur case it would probably make more sense to just retrieve the value from the control, eg:

Code: Select all

Entry.Add("Button", Posit, Name).OnEvent("Click", Func('Handle'))

Handle(GuiCtrlObj, *) {
	Name := GuiCtrlObj.Text
}

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 2 guests