Menu ID

Hi All,
I’m creating drop down menus for a listbox. I’ve used the following code from the examples:

Var base As New DesktopMenuItem
base.AddMenu(New DesktopMenuItem("Red"))
base.AddMenu(New DesktopMenuItem("Green"))
base.AddMenu(New DesktopMenuItem("Blue"))
base.AddMenu(New DesktopMenuItem("Black"))
base.AddMenu(New DesktopMenuItem("White"))

Var selectedMenu As DesktopMenuItem
selectedMenu = base.PopUp

Obviously I’ve put my own stuff in the actual menu but this is the basic coding I’ve used. What I’d like to know, if someone can help, is how can I get an identifier (other than the text) for the actual selection. Ie, assuming they are numbered 0-4 in the menu array, how do I get that number.

selectedMenu.Text correctly returns the actual text of the selection. I’ve then tried selectedMenu.Index but that returns a long 6 or 7 digit number and is the same one regardless of the selection. I then thought maybe I could store a number for the position in the array under “Tag” but couldn’t figure the correct code for that and where I put it in.

I hope all that makes sense, as always, thanks for any help.
Barry

You need to make individual menuitems and not with “new menuitem” like this:

dim PopUpMenuItem as new MenuItem
dim MailMenuitem as new MenuItem(kImportMail)
MailMenuitem.Icon = getApplicationIcon("com.apple.mail")
MailMenuitem.Enabled = not (MailMenuitem.Icon = Nil)
MailMenuitem.Tag = "com.apple.mail"
PopUpMenuItem.AddMenu(MailMenuitem)

You can also keep your short version, and add the optional parameter (representing the tag, as a variant). E.g.:

Var base As New DesktopMenuItem
base.AddMenu(New DesktopMenuItem("Red",Color.Red))
base.AddMenu(New DesktopMenuItem("Green",Color.Green))
base.AddMenu(New DesktopMenuItem("Blue",Color.Blue))
base.AddMenu(New DesktopMenuItem("Black",Color.Black))
base.AddMenu(New DesktopMenuItem("White",Color.White))

Var selectedMenu As DesktopMenuItem
selectedMenu = base.PopUp

(or use a string identifier (or anything else) if you prefer)

1 Like

Hi Arnaud,
Thanks for the reply.

So, my actual code more accurately is along these lines:

Var base As New DesktopMenuItem
Var A() As String = array("A", "B", "C", "D", "E")
For i As Integer = 0 To A.Count - 1
  base.AddMenu(New DesktopMenuItem(A(i))
Next

Var selectedMenu As DesktopMenuItem
selectedMenu = base.PopUp

So, if I’m following correctly then, changing the assigning line to the following

base.AddMenu(New DesktopMenuItem(A(i), i)

will mean that I can access the position in the array with

selectedMenu.Tag

If I choose the 3rd element
selectedMenu.Text will return C
and
selectedMenu.Tag will return 2

Is that correct?

Barry

1 Like

Hi Beatrix,
Thanks for the reply. I’ll have to work through all of that to see if I can work it out. It looks at first glance tho as if you are saying the coding from the example app is not the correct way of doing it?

Barry

The way in the example is correct. You just can’t change the tag that way.

i usually created a menu item with optional argument tag
and in the click event i made a select case on this tag (neutral english name of the menu)

you can also store any object in the tag

Yes, that’s correct.
You can store whatever you want in the tag (as long as each item has the same, predictable, kind of data), so an integer representing the index is fine (just remember to not remove items in the middle without renumbering).

BTW: I find odd the DesktopMenuItem class doesn’t have some kind of “IndexOf” function to directly find the index of a menu item inside its parent menu (similar to Array.IndexOf, so one could just write Index=Base.IndexOf(selectedMenu) instead of storing the index in the tag (which seems a duplicate information in the underlying menu system).

Hi again,
yeah that works perfectly.

As for your BTW, I agree but I also don’t get why the 'index" property doesn’t return what I thought it would, but hey, a lot of things don’t return what I think they will :thinking:. Luckily you guys are here :slight_smile:
It also baffles me that some of the answers here (like this one) just don’t appear to be in the docs.

As far as I can see, there’s no reference to the “optional” extra tag parameter.

Anyway, as always thanks again everyone :slight_smile:
Barry

It is under Constructor DesktopMenuItem — Xojo documentation

The answers here are using the New keyword, which fires the Constructor. The AddMenu part just takes the newly constructed menuitem and puts it in the menu.

Hi Tim,
Yeah thanks, I see it. With my “limited” understanding of some of the more “intricate” aspects of programming, I would never have thought to look there - basically coz I’m not up with things like constructor etc :roll_eyes:

Thank god for you guys & your patience :hugs:

Barry

1 Like

The confusion with the Index property is discussed in this feedback case I opened a couple of years ago. If you’re interested, feel free to check out the discussion.

https://tracker.xojo.com/xojoinc/xojo/-/issues/69672

1 Like

you can use this (theoretical) with index / your menu enumeration value
AddMenuAt(index As Integer, item As DesktopMenuItem)

and in the menu event there is DesktopMenuItem.Index

the constructor with tag was a feature request i made :slight_smile:

Thanks again for all the info guys. Just whilst on ListBoxes, another quick question - I know I can set the list box so I can enter text into the cells, is there a way I can apply masks to those cells? - eg ##-##-#### for date entry etc. (btw, that’s Australian dates :slight_smile: )

Barry

Yes. I think so.

Yes, you can implement this in the CellFocusReceived event:

Sub CellFocusReceived(row as Integer, column as Integer) Handles CellFocusReceived
  me.ActiveTextControl.ValidationMask="###" //Only allow 3 digits
End Sub

Hi Arnaud,
Thankyou for that (and Emile for your suggestion too :slight_smile: ).

That is fantastic!. Can I just ask tho, for my understanding, your code says:

Sub CellFocusReceived(row as Integer, column as Integer) Handles CellFocusReceived
  me.ActiveTextControl.ValidationMask="###" //Only allow 3 digits
End Sub

I can find an event for the Listbox called CellFocusReceived. Since putting the active line here works perfectly, I don’t understand why you have put it the way you have with a different name that has an extra bit - “Handles CellFocusReceived”

Is there an advantage or something doing it that way? What am I missing?

Sorry to be a nuisance and keep it going, feel free to give up and thanks for what you’ve given so :grinning_face:

Barry

Well, that’s not from me; copying an event from the IDE simply does that (it’s an internal convention).

No problem at all.

Yu would use only this line of code. The IDE automatically supplies the other 2.

Hi Tim,
Yeah thanks, that’s what Arnaud said. That actually explains how a few times in the past I’ve seen code like that and never been able to understand it. Little tid-bits make a huge difference when you’re just an amateur. :slight_smile:

Barry