2

I have tool bar in my app and context menu with the same options, so I want to add ToolStripButtons to both ContextMenuStrip and ToolStrip, unfortunately I can't do this. Even when I add manually items to both it shows only on one.

For now I have buttons in tool bar:

I want something like this. I want this options to be one, because I will be often enable and disable this buttons and finally there is one option so why two buttons?

Mohsen Safari
  • 6,669
  • 5
  • 42
  • 58
  • if the menua aren't dynamic you could simply build the 2 separatly and than make a fonction to syncronize them – Rémi Apr 18 '13 at 11:52
  • I just make items Disable manually: toolStripItem1.Disabled = true; contextStripItem1.Disabled = true; for all items I have, for now. – Kamil Szymon Apr 18 '13 at 13:05
  • then I don't really see to problem. you can still make the equivalent button have the same eventhandler so both will do the same thing – Rémi Apr 18 '13 at 14:05
  • Yes, I know but both has same name, same image, same behavior so I'm thinking that should be same button. but If it is not possible, I just repeat every disable, enable, visible etc. for both. – Kamil Szymon Apr 18 '13 at 14:10
  • It's not possible. A control can't be in two places at once. You really have no option but to double up. This is quite common. – Michael Mankus May 16 '13 at 18:14

1 Answers1

0

This is a common problem and I've found the easiest solution is to place the 'shared' code inside a MenuFeature class that inherits from ToolStripMenuItem.

You still have to create 2 instances of this class, but each instance is very lightweight and only has code for any differences between the 2 usages (i.e. the ContextMenu item might use ToolStripItemDisplayStyle.ImageAndText, while the ToolStrip item might use ToolStripItemDisplayStyle.Image).

This allows common code to exist only once inside your custom MenuFeature class, but still allows changes local to each usage of this menu item.

If you wanted to automatically synchronize properties like Enabled/Visible/etc, you could maintain a static collection of all instances inside the constructor, and then update all the items using events like EnabledChanged/etc. I would recommend against this, however, as I've found different instances of the same menu 'feature' often need their own state - but this is getting out of scope on this Question, those interested in how I've managed items can comment on this answer or PM me.

Thracx
  • 403
  • 5
  • 8