Toolstrip menu items in C# 2.0

L

Ludwig

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!
 
D

Dan Manges

Ludwig said:
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
 
C

chanmm

To me I will be lazy to just creat a method the but of the toolstrip menu
and context menu call that method.

chanmm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top