473,414 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

How to Share a ToolStripMenuItem

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());
}
}
Oct 10 '07 #1
3 9152
Martijn Mulder wrote:
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.
Oct 10 '07 #2
Chris Shepherd schreef:
Martijn Mulder wrote:
>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.
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?)
Oct 10 '07 #3
Martijn Mulder wrote:
>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.
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.
Oct 11 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Maileen | last post by:
Hi, I've create an application which includes : - a toolstrip - a toolstripmenu - several toolstripmenuitem(s) so, now i have a menu like that : -File
2
by: genojoe | last post by:
I have a VB 2003 project that I have updated to 2005. I notice that MenuItem is being phased out and replaced by ToolStripMenuItem. I want to investigate the new capabilities of this new control...
0
by: genojoe | last post by:
I have a menu structure that formats that left aligns string1 and right aligns string2. To do this, I use the OwnerDraw, Popup, DrawItem and MeasureItem properties or events of the MenuItem class....
1
by: =?Utf-8?B?RjVGNUY1?= | last post by:
I have created a control by inheriting ToolStripMenuItem that has an image property called LargeImage. ------ public class EventSubMenuItem : ToolStripMenuItem { private Image _largeImage =...
1
by: Justin Cherniak | last post by:
I have a ToolStrip item which fires a function when clicked. The function will run to completion without any problems (if I set a breakpoint at the closing brace, no exception has been fired), but...
3
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
I need to show a custom control in the DropDown of a Windows.Forms.ToolStripMenuItem (e.g., similar to the Font Color menu item in Word except that the control is specific to my application). I...
2
by: in10se | last post by:
I am trying to mimic the functionality of a web browser's "Favorites" (or "Bookmarks") menu. I would like to be able to right-click on a ToolStripMenuItem and have a context menu appear so that I can...
0
by: Dean Slindee | last post by:
I need to be able to separate toolstripmenuitems from toolstripseparators, both of which are children of a toolstripdropdropbutton. The last statement does not pass validation. Is there any way...
0
by: kyungdongkim | last post by:
Hi, I have a dynamically generated MenuStrip following this example: http://www.codeproject.com/useritems/Dynamic_MenuStrip.asp Basically the menu strip allows users to save and load reports. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.