Creating a menu with a loop not possible with v2 anymore? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
peterbonge
Posts: 8
Joined: 02 May 2015, 15:03

Creating a menu with a loop not possible with v2 anymore?

Post by peterbonge » 21 Mar 2023, 15:27

In the past I've used different kind of loops to create menus (inclusive submenus). Now I'm trying this with v2, but I don't get it to work.

I'm struggling at this point:

Code: Select all

MyMenuSub1 := Menu()
MyMenuSub1.Add SubName, MenuHandler
The problem is "MyMenuSub1". I need to replace "Sub1" with a variable, but "MyMenu%SubName%" doesn't work.

Has anybody a solution?

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Creating a menu with a loop not possible with v2 anymore?

Post by swagfag » 21 Mar 2023, 16:56

sound to me more like ure trying to do something that is no longer necessary to do in v2 and it doesnt work
best show the v1 code

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

Re: Creating a menu with a loop not possible with v2 anymore?

Post by mikeyww » 21 Mar 2023, 16:57

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v2.0
Persistent
m := []

Loop 2
   m.Push(Menu())
 , m[A_Index].Add('Test' A_Index, go)
 
m[1].Show()
m[2].Show()

go(ItemName, ItemPos, MyMenu) {
 MsgBox ItemName ': Position = ' ItemPos
}

peterbonge
Posts: 8
Joined: 02 May 2015, 15:03

Re: Creating a menu with a loop not possible with v2 anymore?

Post by peterbonge » 22 Mar 2023, 02:01

Imagine you are looping through a folder with shortcuts and subfolders with shortcuts and want to create a menu of it. Here is a reduced sample v1 code:

Code: Select all

ItemsList := "
(
Sub1,Sub Item 1a
Sub1,Sub Item 1b
Sub1,Sub Item 1c
Sub2,Sub Item 2a
Sub2,Sub Item 2b
,Main Item 1
,Main Item 2
,Main Item 3
,Main Item 4
)"

SubList := ""
SubName := ""
ItemName := ""

Loop, Parse, ItemsList, `n, `r
{
	ItemsArray := StrSplit(A_LoopField, ",")
	SubName := ItemsArray[1]
	ItemName := ItemsArray[2]

	If (ItemName) {
		Menu, ItemsMenu%SubName%, Add, %ItemName%, MenuHandler
	}

	If (SubName != "" And InStr(SubList, SubName) = 0) {
		SubList := SubList . SubName . "`n"
		Menu, ItemsMenu, Add, %SubName%, :ItemsMenu%SubName%
	}
}

Menu, ItemsMenu, Show
Return

MenuHandler(ItemName) {
	MsgBox, % 0+64+0+4096, Info, You selected %ItemName%
}

just me
Posts: 9450
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Creating a menu with a loop not possible with v2 anymore?  Topic is solved

Post by just me » 22 Mar 2023, 04:50

Code: Select all

#Requires AutoHotkey v2.0

ItemsList := "
(
Sub1,Sub Item 1a
Sub1,Sub Item 1b
Sub1,Sub Item 1c
Sub2,Sub Item 2a
Sub2,Sub Item 2b
,Main Item 1
,Main Item 2
,Main Item 3
,Main Item 4
)"

SubName := ''
ItemName := ''
ItemsMenu := Map()
ItemsMenu.CaseSense := "Locale"
ItemsMenu[''] := Menu() ; the main menu is stored with key ''

Loop Parse, ItemsList, "`n", "`r" {
	ItemsArr := StrSplit(A_LoopField, ",")
	SubName  := ItemsArr[1]
	ItemName := ItemsArr[2]
   If (SubName != '') && !ItemsMenu.Has(SubName) {
      ItemsMenu[SubName] := Menu()
      ItemsMenu[''].Add(SubName, ItemsMenu[SubName])
   }
	If (ItemName)
		ItemsMenu[SubName].Add(ItemName, MenuHandler)
}

ItemsMenu[''].Show()
Return

MenuHandler(ItemName, *) {
	MsgBox("You selected" . ItemName, "Info", 0+64+0+4096)
}

peterbonge
Posts: 8
Joined: 02 May 2015, 15:03

Re: Creating a menu with a loop not possible with v2 anymore?

Post by peterbonge » 22 Mar 2023, 05:41

@just me Thanks!
This works really well.

Post Reply

Return to “Ask for Help (v2)”