How do I determine which menubar menu item I am using an index?

I have an array of five menu items, but I can’t seem to figure out how to determine which one was selected. Is there an index system or case select system of some sort?

How did you create the menuitems? If you created them in the IDE, then you should already know the index of each one. If you create them in code, you’ll have to store enough information to identify the items. Perhaps create a string array and store the text of each menuitem in the corresponding index slot.

You can store an Integer inside MenuItem.Index which you could use to look up the entry or store Variant information (like a string) inside MenuItem.Tag which you could use directly as your “answer” or use it to store information to lookup when the selection is made.

Thank you!

I don’t see where you have access to anything but the index in a menu handler.

Inside the Action event you have access to Self which is a MenuItem of the selected entry, from that you can read:

Me.Index Me.Name Me.Tag

which can all be used to retrieve/pass useful info. These properties can all be changed/set at runtime, even Index which the docs say is a read-only property.

That’s what I thought, but it doesn’t seem to be working. I’ll try it again.

Might be different on mac/windows, I’m testing in windows here.

I’m testing on Windows, too. In the menuhandler, ME is the window, not the menuitem.

Self (doh) let me edit my posts up there if I can

Um, Self is also the window.

Are you using a menuitem subclass? Or just adding menu items in the IDE?

I’m just playing around with the Examples>Desktop>Menus>FontMenu example, so a Class with a MenuItem Super, with an Action event added to it.

Ah, that explains it. If you don’t use a class, you’re out of luck.

Inside the class code:

Me = Self = the menuitem

If you just add a menuitem to a menu in the IDE and then add a menuhandler to the window, then in the menuhandler:

Me = Self = the window

Surely if you used a straight menu handler added to a window then it would be handling the entry you assign it in the IDE so there wouldn’t be much use to finding the selected item as the called handler is only called because the entry has been clicked? I assumed that Matthew was using code to generate his menu and so wasn’t using this method.

You can use the IDE to create a menuitem array, which is what I assumed he was doing. All the menu items call the same menu handler and pass in an Index, just like a control array. However, at that point, all you have to go in is the index value. You can’t access any of the other values of the menu item.

And that’s why I asked which way he was doing it, because there is a big difference.

I’m not sure how you do that because I can only see two types of menu handlers, static that you make at design time, one per menu entry you want to handle, and dynamic created at runtime in the way I mentioned above, is there a third way?

It’s the same way you create a control array. You set the MenuItem name to be the same as another MenuItem, and the IDE sets the Index value for you. Or you can set the Index value to whatever you want.

Ahha, I see now, crikey that’s well hidden, I wouldn’t have found that as that isn’t the way I make control sets, I use the interface on the gear tab. I can’t find a mention of that in the docs at all under desktop menus.