Why not?

Hi All,
Can someone tell me why this won’t work please?

I am trying to build a popup menu when clicking on a cell in a DesktopList control. This is all new to me. I have in the MouseUp event:

Var base As New DesktopMenuItem
base.AddMenu(New DesktopMenuItem(“Plane”))
base.AddMenu(New DesktopMenuItem(“Train”))
base.AddMenu(New DesktopMenuItem(“Bus”))
base.AddMenu(New DesktopMenuItem(“Car”))
base.AddMenu(New DesktopMenuItem(“Boat”))
base.AddMenu(New DesktopMenuItem(“Shared Transfer”))
base.AddMenu(New DesktopMenuItem(“Private Transfer”))

This works fine. But, if I replace it with:

Var base As New DesktopMenuItem
for i As Integer = 1 To App.Sub_Type_Title.Count
base.AddMenu( New DesktopMenuItem( App.Sub_Type_Title) )
next

Where App.sub_Type_Title is an array of all of those terms as a property of the App, it comes back with an error “that there is more than 1 method with this name but this does not match any of the available signatures”. That array is used in multiple areas of the app which is why I have it stored here and is recognised all other times I’ve referenced it.

It has me stumped why it won’t work. Any suggestions appreciated.

Thanks,
Barry

Try adding the array index: base.AddMenu( New DesktopMenuItem( App.Sub_Type_Title(i)) )
Not at my computer right now to test but that should work.

Of course that’s the answer!! Geeze, sometimes you just stare and stare and the answer is sitting there looking at you. Thanks.

Perhaps you can help me with the next bit then, please.
I have to now determine the result. Note that as this is new to me, a lot is straight out of the docs.

If I put:
Var selectedMenu As DesktopMenuItem
selectedMenu = base.PopUp

If selectedMenu <> Nil Then
me.CellTextAt(row,col) = selectedMenu.Text
End

it will display the text of the array but I want a pointer to the actual position in the array. I would have thought it would be

selectedMenu.Index

but that returns a result 2147483648 regardless of which item is chosen. How do I find the actual item in the menu array? I want to be able to save that in a database.

Thanks

Not anywhere near an expert myself but you could just check the text and return an array index:

var base As New DesktopMenuItem
var arr() As String = Array(“One”,“Two”,“Three”)
for i As Integer = 0 to arr.Count - 1
base.AddMenu(New DesktopMenuItem(arr(i)))
Next

var selectedItem As DesktopMenuItem
selectedItem = base.PopUp
Select Case selectedItem.Text
Case “One”
Return 0
Case “Two”
Return 1
Case “Three”
Return 2
End Select

My guess is that there is a much more elegant way to do this and perhaps someone else on the forum can enlighten us. I also got the “2147483648” value which doesn’t make sense as an array index here which confirms my feeling of "not understanding’ the topic.

Hi William,
Thanks for the suggestion. That would work for a static array as I have used for the demo here but there is another couple of fields where the menu is built from entries in a database. Each time I click on that cell I use code such as:

var rs = DB.select(“select * from XXXX where ID = YY”)
(*note that ‘DB’ here is a defined SQLite database)

Then I populate the menu with the returned entries. So that number will change as the entries in the database change.

Thanks for your help so far tho, at least I’ve got the menu building working, just have to find a way to determine what was selected and store that value.

:slight_smile:

Try using the array.IndexOf(text of menuItem) to get an index to you array:

var base As New DesktopMenuItem
var arr() As String = Array(“One”,“Two”,“Three”)
for i As Integer = 0 to arr.Count - 1
base.AddMenu(New DesktopMenuItem(arr(i)))
Next
var selectedItem As DesktopMenuItem
selectedItem = base.PopUp

Var arrIndex As Integer = arr.IndexOf(selectedItem.Text)
MessageBox(arrIndex.ToText) // this returns the array index

William,
Thanks for all your help. I didn’t know about arrayOf but all this has been a learning experience and, as a hobbiest, that’s where the “fun” is.

Anyway, this is what I ended up with:

Var base As New DesktopMenuItem

var rs as rowset = App.DB.SelectSQL(“select * from SOURCE where TYPE = 1 or TYPE = 3 order by NAME”)
var arrN() As String
var arrID() As Integer
for Each r As DatabaseRow in rs
base.AddMenu( New DesktopMenuItem( r.Column(“NAME”) ) )
arrN.Add(r.Column(“NAME”).StringValue)
arrID.Add(r.Column(“ID”).IntegerValue)
next

Var selectedMenu As DesktopMenuItem
selectedMenu = base.PopUp
Var arrIndex As Integer = arrN.IndexOf(selectedMenu.Text)
me.CellTextAt(row,col) = selectedMenu.Text
me.CellTagAt(row, col) = arrID(arrIndex)

I don’t know from a pro’s perspective if it is elegant or not but, it works! It displays the text in the listbox cell and stores the ID or the array number in the cellTagAt property. I can then read that later and save it to the database when needed.

Awesome, thanks :slight_smile:

Great! I’ve spent many hours over the years coding for “elegance”; now older and wiser I opt for getting things done quickly in ways that just plain work. Good luck with your hobby.

1 Like

DesktopMenuItem have also an constructor with text and tag.
you could store your id in this tag.
https://documentation.xojo.com/api/user_interface/desktop/desktopmenuitem.html#desktopmenuitem