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)
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
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?
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).
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 . Luckily you guys are here
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.
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
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.
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 )
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 ).
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
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.