Problem With Dynamically Generated TrayMenu Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Larkal
Posts: 21
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: 21
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 “Ask for Help (v2)”

Who is online

Users browsing this forum: Bing [Bot], emp00, macromint and 89 guests