Search code examples
.netwinformsvb6-migrationtoolstripmenustrip

Is there a way to make A ToolStripMenuItem shared between Menus and ToolBars?


I'm porting an old VB6 project to C#, using Winforms. The original uses DataDynamic's ActiveBar suite to manage changes to multiple items in toolbars, menus, and shortcut ribbons simultaneously. All the old code has to do to change the text of an item shared between several menus, a toolbar, and a context menu is change the "tool" object's "Caption" property.

I feel like .NET must have some way of sharing ToolStripItems between a variety of containers, but it doesn't look like it does. Am I missing something?

Currently I've started several times with a variety of ideas, from implementing a Clone extension method on ToolStripMenuItem objects and having my form keep track of each sharable. That failed when I realized that updating the local copy wouldn't update the others (I'd have to add complex updating events that is more work than simply turning each item on and off by hand). I've considered creating some way of updating all items based on its tag. That seemed pretty infeasible as well.

What techniques have y'all used in similar situations?


Solution

  • Well, this is actually one of the annoying limitations of .Net GUI-Building Libraries

    A common workaround would be managing your Menus dynamically, adding Items to them as they are opening. Ugly but quite seamless from the user experience point of view. The trick is to use the following UI design pradigm : Menus are never displayed simultaneously, so you can hot-swap Items

    private void myContextMenuStrip_Opening(object sender, CancelEventArgs e)
    {
        myContextMenuStrip.Items.Insert(3, myToolStripMenuItem);
    }  
    

    Then using flags and code logic you would be able to know, As the Menu/ToolBar/ContextMenu is opened, what Items it should display.