[V2-β7](Gui) How to bind variables to callback function inside a loop Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
hoangthi
Posts: 16
Joined: 08 Oct 2020, 11:00

[V2-β7](Gui) How to bind variables to callback function inside a loop

Post by hoangthi » 11 Aug 2022, 04:46

Hi, I've just added a couple of text fields into my GUI using a Loop function.
Each text field has a OnEvent Callback function to retrieve its value whenever I click on it.

The issue is, that I can't bind variables inside the Loop to the call back function. No matter what is it in the Loop, it will only return 0 inside the callback.

The only workaround so far is to pass a Ctrl Object to the call-back function, then retrieve each Text Control Name and Text

Could you please tell me a way to bind variables (sorry for lack of a better word) from the Loop to the CallBackFunc()?

Here is my example:


image.png
image.png (38.2 KiB) Viewed 960 times

Code: Select all

myGui := Gui()

loop 10 
{

myText := myGui.Add("Text", "h33 w100 Left BackgroundTrans 0x201 vTextCtrl" . A_Index, "Hello world" . A_Index)


myText.OnEvent("Click", CallBackFunc)

	CallBackFunc(CtrlObj, *)
	{
		MsgBox("Ctrl Name (correct): " . CtrlObj.Name . "`r`n Variables inside the loop (wrong): " . A_Index)
	
	}

}

myGui.Title := "Testing Callback function"

myGui.Show("AutoSize")

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [V2-β7](Gui) How to bind variables to callback function inside a loop  Topic is solved

Post by AHK_user » 11 Aug 2022, 06:41

Simple, just bind the variable to the callback function.

Code: Select all

#SingleInstance force
#Requires Autohotkey v2.0-a+

myGui := Gui()

loop 10 
{

myText := myGui.Add("Text", "h33 w100 Left BackgroundTrans 0x201 vTextCtrl" . A_Index, "Hello world" . A_Index)


myText.OnEvent("Click", CallBackFunc.Bind(A_Index))

	CallBackFunc(Index, GuiCtrlObj, Info)
	{
		MsgBox("Ctrl Name (correct): " . GuiCtrlObj.Name . "`r`n Variables inside the loop (correct): " . Index)
	
	}

}

myGui.Title := "Testing Callback function"

myGui.Show("AutoSize")

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [V2-β7](Gui) How to bind variables to callback function inside a loop

Post by AHK_user » 11 Aug 2022, 06:44

you actually do not need the function, I would advice a fat arrow function, but maybe it is less readable:

Code: Select all

#SingleInstance force
#Requires Autohotkey v2.0-a+

myGui := Gui()

loop 10 {
    myText := myGui.Add("Text", "h33 w100 Left BackgroundTrans 0x201 vTextCtrl" . A_Index, "Hello world" . A_Index)
    myText.OnEvent("Click", ((Index, GuiCtrlObj, Info) => (MsgBox("Ctrl Name (correct): " . GuiCtrlObj.Name . "`r`n Variables inside the loop (correct): " . Index))).Bind(A_Index))
}

myGui.Title := "Testing Callback function"
myGui.Show("AutoSize")

hoangthi
Posts: 16
Joined: 08 Oct 2020, 11:00

Re: [V2-β7](Gui) How to bind variables to callback function inside a loop

Post by hoangthi » 11 Aug 2022, 07:47

AHK_user wrote:
11 Aug 2022, 06:41
Simple, just bind the variable to the callback function.

Great! I didn't think it was that simple. I am very grateful!

instead of

Code: Select all

myText.OnEvent("Click", CallBackFunc.Bind(A_Index))
I tried (wrong way)

Code: Select all

myText.OnEvent("Click", CallBackFunc(A_Index))
and also tried (wrong way)

Code: Select all

myText.OnEvent("Click", CallBackFunc.Bind(A_Index))

CallBackFunc(A_Index, GuiCtrlObj, *) 
{
	MsgBox(A_Index)
}
For a newbie to Autohotkey like me, it is a bit complicated to use vControl & Control, A_Index & Index, ...

Post Reply

Return to “Ask for Help (v2)”