Getting dictionary value by selected menu item Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
dschuller
Posts: 2
Joined: 06 Feb 2023, 06:07

Getting dictionary value by selected menu item

Post by dschuller » 06 Feb 2023, 06:13

Hello,

I try to get a dictionary value by the selected item of a TextMenu.

Code: Select all

dict := { "A" : "123", "B" : "345", "C" : "678" }
txt := dict[A_ThisMenuItem]
MsgBox % txt
I guess I can simply use A_ThisMenuItem as a dictionary key.
Any suggestions?

User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Getting dictionary value by selected menu item  Topic is solved

Post by mikeyww » 06 Feb 2023, 07:24

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v1.1.33

dict := { "A" : "123", "B" : "345", "C" : "678" }
Menu menu, Add, A
Menu menu, Add, B
Menu menu, Show
Return

A:
B:
txt := dict[A_ThisMenuItem]
MsgBox % txt
Return
If you are new to AutoHotkey, you can work with the current version instead of the old version.

Code: Select all

#Requires AutoHotkey v2.0

dict := Map("A", 123, "B", 345, "C", 678)
m := Menu()
m.Add("A", go)
m.Add("B", go)
m.Show

go(ItemName, ItemPos, MyMenu) {
 MsgBox dict[ItemName]
}

dschuller
Posts: 2
Joined: 06 Feb 2023, 06:07

Re: Getting dictionary value by selected menu item

Post by dschuller » 06 Feb 2023, 10:15

Thanks for your reply.
I was already on the right track, but I had an issue with my dictionary not being defined as global, but used in a function.
Now it's working.

Post Reply

Return to “Ask for Help (v1)”