How do I dynamically add new Child Bar Menu commands to a top-level BarMenu Item ?

Bars for Windows Forms Forum

Posted 8 years ago by Ian Williams
Version: 16.1.0330
Avatar

I am trying to programmatically add new child items (and links) to my top-level PopupButtonCommand Menu item.

I already inserted a BarPopupButtonCommand (Category = "New Menu") into my UI call 'PlugIns" and using the code below (which works but is probably not the best way), I can enable or disable the button.

                foreach (ActiproSoftware.UIStudio.Bar.BarCommand cmd in barManager.Commands)
                {
                    if (cmd.Tag == "PlugIns"){
                        if (plugIns == null || plugIns.Length == 0)
                            cmd.Enabled = false;
                        else
                        {  
                            // populate with our available plug-ins
                        }
                    }
                }

 I am using an interface to create different plug-ins for my application. After reading a folder and getting a list of available plug-ins, I'm trying to add those plug-in items (with command links) programmatically to my current barManager.Commands.

After looking at all the ActiPro examples, I'm still struggling.   Any example code or basic step-by-step would be great.

Thanks, Ian

Comments (4)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Ian,

I think what you might be missing is that you can try-cast your command to BarPopupButtonCommand as that is the command used for menu items that can have other menu items.  That type has a DefaultCommandLinks that you can populate with command links for all your plugins, one for each plugin. 

You might want to have a normal BarButtonCommand registered on the BarManager for a general plugin menu item.  Then use command links created from that for your BarPopupButton.DefaultCommandLinks.  For each command link, perhaps you put some reference to each plugin in the Tag property.


Actipro Software Support

Posted 8 years ago by Ian Williams
Avatar

Thanks heaps - that makes sense.   However, after adding some code to create and add a link to the top-level menu item "New Menu.PlugIns", I would have expected the below added links to show up as child items.  They do not.   Am I still missing something?

  
foreach (string s in plugIns) {
   // Create a link
   BarCommandLink link = 
       ((BarPopupButtonCommand)barManager.Commands["New Menu.PlugIns"]).CreateCommandLink();
   link.Text = s;
   link.Tag = s;
  // Add the link to the barManager 
  ((BarPopupButtonCommand)barManager.Commands["New Menu.PlugIns"]).DefaultCommandLinks.Add(link);
}
  
 
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Ian,

Hmm... it could be that the command link for your main Plugins command has already been created.  The command.DefaultCommandLinks only really gets used when the command link is created, which if the above is true, would be too late.  What you'd need to do in that case is find the command link that is already created in the UI and update its BarPopupButtonLink.CommandLinks collection instead.  That would alter the children of the already-created command link.


Actipro Software Support

Posted 8 years ago by Ian Williams
Avatar

Ok, good tip.  I'll give that a try.

Thanks!

The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.