I created a MenuBar (MyMenuBar) . The normal File and Edit + 2 menus in the menubar : Instrument and MidiDevice.
In the Instrument menu I try to add some menuItems from within the program with the following code:
Var editPasteSpecial As New MenuItem
editPasteSpecial.Text = “Paste Special…”
EditMenu.AddMenuAt(5, editPasteSpecial)
InstrumentsList = “GENOS;TYROS 4;TYROS 5;TYROS 3;PSR S550”
Var mi As New MenuItem
For i As Integer = 0 To InstrumentsList.CountFields(";")
mi.Text = InstrumentsList.NthField(";", i)
InstrumentMenu.AddMenuAt(i, mi)
Next
Var testSpecial As New MenuItem
testSpecial.Text = “Test Special”
InstrumentMenu.AddMenuAt(3, testSpecial)[/code]
The result is ::
The first addMenuAt inserts correctly the Paste Special menu into the EditMenu.
Then the items that I try to insert to the InstrumentMenu has as result (this is my problem):
PSR S550 - > this item is not selectable in the menu and shows up twice in the list ???
GENOS -> Selectable
TYROS 4 -> Selectable
TYROS 5 - > Selectable
TYROS 3 -> Selectable
PSR S550 -> Selectable
The next insert is a Test Special in the InstrumentMenu -> this is inserted correctly in the InstrumentMenu
Is my code wrong or is there a bug in the AddmenuAt function ?
PS. Is it possible to send a image in a conversation ? Not via a link but directly into the conversation.
Var mi As New MenuItem
For i As Integer = 0 To InstrumentsList.CountFields(";")
mi.Text = InstrumentsList.NthField(";", i)
InstrumentMenu.AddMenuAt(i, mi)
Next
as you only create ONE item then change the text many times
And you try to insert it several times
It should create a new instance each time
For i As Integer = 0 To InstrumentsList.CountFields(";")
Var mi As New MenuItem
mi.Text = InstrumentsList.NthField(";", i)
InstrumentMenu.AddMenuAt(i, mi)
Next
EDIT : none of these will be enabled unless you have one of
you have a menu handler for the item that matches its NAME and the menu item is set to autoenable
each of thee is a subclass of menu item and they have an Action event handler implemented as part of the subclass
Thanks Norman.
This does not solve my problem completely.
The result is now that the there are 6 items inserted (as before : 1 too much) but the first one is not PSR S550 but a empty place in the menu.
AND none of the items is selectable.
The loop is wrong
NthFields numbers fields from 1 to N not 0 to N
For i As Integer = 1 To InstrumentsList.CountFields(";") // NOTE CHANGE FROM 0 to 1
Var mi As New MenuItem
mi.Text = InstrumentsList.NthField(";", i)
InstrumentMenu.AddMenuAt(i, mi)
Next
Norman beat me in on the 1-based numbering for NthField. I would also recommend using NthFieldB to save a few processing cycles since it’s a known, single-byte value you’re using as your separator.
Thanks.
Little correction in line 4 because AddMenuAt position is zero-based.
For i As Integer = 1 To InstrumentsList.CountFields(";")
Var mi As New MenuItem
mi.Text = InstrumentsList.NthField(";", i)
InstrumentMenu.AddMenuAt(i-1, mi)
Next
PS. I thought I had read somewhere that everything in XOJO had become 0-based (API 2) ?
Now I need to found out how to enable the created menu items.