By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,704 Members | 1,475 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,704 IT Pros & Developers. It's quick & easy.

Toolstrip menu items in C# 2.0

P: n/a
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 Stuyck
http://www.coders-lab.be
Jun 7 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jun 7 '06 #2

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

chanmm

"Ludwig" <no**@none.com> wrote in message
news:1e********************************@4ax.com...
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 Stuyck
http://www.coders-lab.be

Jun 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.