AddMenuAt ?

XOJO 2019r2.1
Windows 10 program

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:

[code]Var InstrumentsList As String

Window2.Left = (screen(0).Width - Window2.Width) / 2
Window2.Top = (screen(0).Height - Window2.Height) / 2

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.

Regards
Etienne

switch this loop around

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

  1. you have a menu handler for the item that matches its NAME and the menu item is set to autoenable
  2. each of thee is a subclass of menu item and they have an Action event handler implemented as part of the subclass

For #1 you can actually set up menu handlers for which there IS no menu item so you can add menu handlers “ahead of time” if you want
See https://www.great-white-software.com/blog/2019/06/28/dynamic-menu-handling/

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.

Regards
Etienne

Right
I addressed this as well

[quote=465980:@Norman Palardy]
EDIT : none of these will be enabled unless you have one of

  1. you have a menu handler for the item that matches its NAME and the menu item is set to autoenable
  2. each of thee is a subclass of menu item and they have an Action event handler implemented as part of the subclass

For #1 you can actually set up menu handlers for which there IS no menu item so you can add menu handlers “ahead of time” if you want
See https://www.great-white-software.com/blog/2019/06/28/dynamic-menu-handling/[/quote]

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.

Regards
Etienne

This begs a new question - CAN we add a menu handler in code since it’s just a Method with a specific type:

[code] #tag MenuHandler
Function selfchgpswd() As Boolean Handles selfchgpswd.Action

                    Break
                    Return True

            End Function
    #tag EndMenuHandler

[/code]
If not, WHY not? Is there a technical issue, or just a “we didn’t implement that” issue?

no
you cant add one that way

now IF you subclassed menuitem then you could add a handler using addhandler for the action event of the item

I’ve done that when needed