ToolBar: filling dynamically a ToolStyleDropDown button

In a ToolBar, I set a ToolStyleDropDown button and place code in Open. Below is the relevant code:

[code] For LoopIdx = 1 To ImgCnt
// Get a reference to an image file (located in gCurrentFI)
imgFI = gCurrentFI.TrueItem(LoopIdx)
If imgFI = Nil Then Return
Dim myImgMI As MenuItem // *

myImgMI = myMenu.Clone
myImgMI.Text = CurrentImageFI.Name
myMenu.Append(myImgMI)

// Close it
' myImgMI.Close // **
myImgMI = Nil // ***

// To avoid 1, Infinite Loop…
If UserCancelled Then Exit

Next
[/code]

() I tried to set this line out of the for next loop.
() This does not allows the Popup Menu to… dropdown!
(
) This allows the Popup Menu to dropdown, but: read entry 2 below.

  1. I do not know that we can add sub-MenuItems (I got three sub-levels) in ToolBar’s ToolItem…


Get an eye at the screen shot… and think about how many entries can be in the second MenuItem, third and fourth… they are many !!!

  1. The code above creates the correct number of entries, but also add sub entries after the first entry, recursively (if four entries, three entries have sub entries, the fourth entry have three sub-entries).
    • Entry 1
    • Entry 2 (as a Sub-MenuItem with 1 entry)
    • Entry 3 (as a Sub-MenuItem with 2 entries, one of them is a standard MenuItem, the other one is a Sub-MenuItem flled with MenuItems)
    etc.

I do not care about 1 and 2, I only want to be able to dynamically add entries in a ToolStyleDropDown’s ToolButton from a ToolBar.

I already have added many entries in a ToolStyleDropDown ToolButton, but in that case, I know the entries to add (no dynamics there) at design time. It is not waht I want here.

PS: the loop to get the files is correct; the code below prove it. (So the problem is not there).

[code] Dim gCurrentFI As FolderItem
Dim imgFI As FolderItem
Dim ImgCnt As Integer
Dim LoopIdx As Integer

gCurrentFI = GetFolderItem("")
If gCurrentFI = Nil Then
Return
End If

// Apply a Header string
LB.Heading(0) = gCurrentFI.DisplayName

// How many Items ?
ImgCnt = gCurrentFI.Count

For LoopIdx = 1 To ImgCnt
// Get a reference to an image file (located in gCurrentFI)
imgFI = gCurrentFI.TrueItem(LoopIdx)
If imgFI = Nil Then Return

// Add an entry…
LB.AddRow imgFI.DisplayName

// To avoid 1, Infinite Loop…
If UserCancelled Then Exit

Next
[/code]
You only need to add a ListBox, name it LB)and put the code above in a PushButton’s Action (in a fresh Project).

As far as I can tell, you don’t properly create the menu items:

myImgMI = myMenu.Clone // this clones the DropDownMenu, kind of recursive... myMenu.Append(myImgMI)

Eli,

it must be because it is early in the morning for me, I do not see a difference in my code with what the code you shared. Care to expand your explanation ?

My code:

myImgMI = myMenu.Clone myImgMI.Text = CurrentImageFI.Name myMenu.Append(myImgMI)

Your code:

myImgMI = myMenu.Clone // this clones the DropDownMenu, kind of recursive... myMenu.Append(myImgMI)

Or my eyes are full of the different attemps o make the code working…

BTW: the code adds menus, but also adds sub in some (never on the first MenuItem) --> the screen shot.

This IS your code - I just removed all lines which don’t matter for the issue you have here.

myImgMI = myMenu.Clone // this clones the DropDownMenu, kind of recursive... myMenu.Append(myImgMI)
Your cloning myMenu, which is a menu item with a submenu, then you add the clone to this very item. What you get is:

myMenu myMenuClone myMenuClone etc.

You need to either clone from another template menu item, or you just create a new one:

[code]For LoopIdx = 1 To ImgCnt
// Get a reference to an image file (located in gCurrentFI)
imgFI = gCurrentFI.TrueItem(LoopIdx)
If imgFI = Nil Then Return
Dim myImgMI As MenuItem // *

myImgMI = New MenuItem()      // <---------------------------- !!!!!
myImgMI.Text = CurrentImageFI.Name
myMenu.Append(myImgMI)

// Close it
' myImgMI.Close // **
myImgMI = Nil // ***

// To avoid 1, Infinite Loop…
If UserCancelled Then Exit

Next[/code]

Hi Eli,

I cannot check the shared modification with the original project (I do not have it where I am), so I re-created the ToolBar piece of my project and it seems to works fine. Of course, I get an entry for all items found in the project folder (invisible, text, Xojo project, etc. and even the Icon I used… because I do not filter them, and I do not try to load the images, not even an error checking ! (I wrote a brand new code, and pasted the one you shared at the correct place).

Note that CurrentImageFI must be replaced with imgFI (in my code, two times above).

My original code was based from the Examples part of MenuItem.Clone

I may be tired yesterday evening when I was working on that.

Thank You Eli for your help.

I will incorporate your change later today when I will be back home.

I re-read the docs (MenuItem.Clone), Example part. It seems that I certainly mis-read it (read it with something else in mind).

Wrong design in my head.
I wanted to use only one MenuItem, based on an example read in the docs and the adaptation design was wrong. Plain wrong.

Thanks again Eli.

I’ve built the applications (OS X, Windows) and the process works fine in both platforms.

I even use two icons (folders): one for OS X, one for Windows XP. Niiiiice.