Ludwig wrote:
My application has a main tool strip menu (File, Options, help,...).
There are also a number of context menu's that popup.
Some main menu items should also be available in some context menu's.
Is there a way of defining these shared menu items once, and merge
them in the main menu and context menu. I don't want to create the
same menu's and handling the same event handlers.
What approach is there for sharing menu items, so that there can be a
central point to manage menu item state and handling menu item events
without redundant code?
Thanks!
Ludwig,
I'm not extremely experienced with WinForms, but I can't really think of
any easy way to accomplish what you want to do.
The best approach I can think of would to be write a method which takes
in a String name, a ContextMenu, a ToolStripMenuItem, and an
EventHandler, and then use that to create the menu items and prevent
repeating yourself in code.
Example:
public void CreateMenuItems(string name, ContextMenu contextMenu,
ToolStripMenuItem toolStripMenuItem, EventHandler handler)
{
contextMenu.MenuItems.Add(new MenuItem(name, handler));
toolStripMenuItem.DropDownItems.Add(new ToolStripMenuItem(name,
null, handler));
}
Now you have the event handler on both menu items, with the menu item
showing up in both places, but only created one place in code.
Hopefully this will help, maybe somebody will know of a better way.
Dan Manges