Problem With Dynamically Generated TrayMenu 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.
Larkal
Posts: 14
Joined: 08 Mar 2019, 00:36

Problem With Dynamically Generated TrayMenu

26 Jul 2020, 22:27

Code: Select all

#SingleInstance Force

A_TrayMenu.Delete ; Remove the default tray menu.

global testArray := [ 21, 32, 54]

Loop testArray.Length
{
    A_TrayMenu.Add "testArray[" A_Index "] = " testArray[A_Index], (*) => WhatsTheNumber(A_Index)
}

A_TrayMenu.Add ; Separator
A_TrayMenu.Add "Exit", (*) => ExitApp()        

WhatsTheNumber(indx) {
    global testArray
    MsgBox "testArray[" indx "] = " testArray[indx]
}
What's wrong with this code? I'm trying to dynamically populate the TrayMenu, but every callback to WhatsTheNumber recieves a value of 0.
Larkal
Posts: 14
Joined: 08 Mar 2019, 00:36

Re: Problem With Dynamically Generated TrayMenu

29 Jul 2020, 00:26

Thanks, I managed to get it working with func/bind.

Code: Select all

#SingleInstance Force

A_TrayMenu.Delete ; Remove the default tray menu.

global testArray := [ 21, 32, 54]

Loop testArray.Length
{
    A_TrayMenu.Add "testArray[" A_Index "] = " testArray[A_Index], Func("WhatsTheNumber").Bind(A_Index)
}

A_TrayMenu.Add ; Separator
A_TrayMenu.Add "Exit", (*) => ExitApp()        

WhatsTheNumber(indx, itemName, itemPos, itemMenu) {
    global testArray
    MsgBox "testArray[" indx "] = " testArray[indx]
}

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: w64bit and 10 guests