How to Share a ToolStripMenuItem

M

Martijn Mulder

My application has a standard MenuStrip on top of the window and a
ContextMenuStrip that pops up when the user clicks the right mouse button.

I have defined a ToolStripMenuItem that I want to appear on both the
MenuStrip and the ContextMenuStrip, but any attempt to do so makes the
ToolStripMenuItem silently disappear from the other menu.

See this little program:



using System.Windows.Forms;

class Form1:Form
{

Form1()
{
ToolStripMenuItem one=new ToolStripMenuItem("one");
ToolStripMenuItem two=new ToolStripMenuItem("two");
ToolStripMenuItem three=new ToolStripMenuItem("three");

MenuStrip menustrip=new MenuStrip();
menustrip.Items.AddRange(new ToolStripItem[]{one,two,three});
Controls.Add(menustrip);

ContextMenuStrip=new ContextMenuStrip();
ContextMenuStrip.Items.Add(two);
}

static void Main()
{
System.Windows.Forms.Application.Run(new Form1());
}
}
 
C

Chris Shepherd

Martijn said:
My application has a standard MenuStrip on top of the window and a
ContextMenuStrip that pops up when the user clicks the right mouse button.

I have defined a ToolStripMenuItem that I want to appear on both the
MenuStrip and the ContextMenuStrip, but any attempt to do so makes the
ToolStripMenuItem silently disappear from the other menu.

I'm dealing with this exact problem right now. I've worked around it by
creating my own class which implements ICloneable so you can at least
assign a Clone of it (including events) to the other menus.

Chris.
 
M

Martijn Mulder

Chris Shepherd schreef:
I'm dealing with this exact problem right now. I've worked around it by
creating my own class which implements ICloneable so you can at least
assign a Clone of it (including events) to the other menus.

My menu is preserving state, i.e. it has Check-marks in it so working
with a Clone means I need to keep the two synchronized... My bet is to
build the menus at runtime, every time it called, from a pool of
ToolStripMenuItems. There never is more than one menu open.

I still have the feeling that what we want is straightforward, easy and
comprehensible. It seems to me that the 'feature' of having only one
ToolStripMenuItem available has been difficult to implement for the .NET
team (and what is it good for?)
 
C

Chris Shepherd

Martijn said:
My menu is preserving state, i.e. it has Check-marks in it so working
with a Clone means I need to keep the two synchronized... My bet is to
build the menus at runtime, every time it called, from a pool of
ToolStripMenuItems. There never is more than one menu open.

Well, I just have a simple method that creates the context menu
dynamically based on another menu when the form is constructed, using
this cloning. You could probably do the same thing, but add methods if
you are adding/removing menus at runtime.

Another method I've seen passed around is to add the menu item tree when
the OnOpening event is fired from each menu, kind of like you're
suggesting. Personally, the method I'm employing works simply because I
don't change the menu after construction. If you did though, it wouldn't
be too difficult to rebuild the menu afterwards, or write a merging method.
I still have the feeling that what we want is straightforward, easy and
comprehensible. It seems to me that the 'feature' of having only one
ToolStripMenuItem available has been difficult to implement for the .NET
team (and what is it good for?)

I think it's related to the fact that each ToolStripMenuItem knows about
its owner.

Chris.
 

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