472,114 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Set up context menus in advance

I've got a ListView which can show many different types of object. I
need to display a ContextMenu whose MenuItems depend on the object
type. I was planning to pre-create the ContextMenus when the app is
started and then assign them when the object is clicked on. The first
few items in the menus
will all be the same (Large Icons, Small Icons, etc.).

I create all of those objects, then add them to the first context menu
and all is well. Then I add them to the second context menu and the
first one has zero items. I don't understand why. Why can't a
MenuItem appear on two different ContextMenus?

Here is part of my code. The first foreach loop returns no items. The
second returns the list of MenuItems I'd expect. Can anyone explain
why and how I avoid this. I didn't want to create specific instances
of each MenuItem for each context menu because I wanted, for example,
to store the currently selected View (Details, Small Icons, etc.) in
the appropriate MenuItem.

private ContextMenu m_DefaultContextMenu;

private ContextMenu m_ComputerContextMenu;

private ContextMenu m_UserContextMenu;

private ContextMenu m_GroupContextMenu;

private MenuItem m_cmnuListViewLargeIcons;

private MenuItem m_cmnuListViewSmallIcons;

private MenuItem m_cmnuListViewDetails;

private MenuItem m_cmnuListViewList;

private MenuItem m_cmnuListViewSeparator1;

private MenuItem m_cmnuListViewBrowseToScript;

private MenuItem m_cmnuListViewSeparator2;

this.m_cmnuListViewBrowseToScript = new MenuItem("&Browse to
script...");

this.m_cmnuListViewBrowseToScript.Index = 0;
this.m_cmnuListViewSeparator1 = new MenuItem("-");

this.m_cmnuListViewSeparator1.Index = 1;

this.m_cmnuListViewLargeIcons = new MenuItem("Lar&ge Icons", new
System.EventHandler(this.mnuViewLargeIcons_Click)) ;

this.m_cmnuListViewLargeIcons.Index = 2;

this.m_cmnuListViewSmallIcons = new MenuItem("S&mall Icons", new
System.EventHandler(this.mnuViewSmallIcons_Click)) ;

this.m_cmnuListViewSmallIcons.Index = 3;

this.m_cmnuListViewDetails = new MenuItem("&Details", new
System.EventHandler(this.mnuViewDetails_Click));

this.m_cmnuListViewDetails.Index = 4;

this.m_cmnuListViewList = new MenuItem("&List", new
System.EventHandler(this.mnuViewList_Click));

this.m_cmnuListViewList.Index = 5;

this.m_cmnuListViewSeparator2 = new MenuItem("-");

this.m_cmnuListViewSeparator2.Index = 6;
this.m_cmnuListViewDetails.Checked = true;
this.m_DefaultContextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuListViewBrowseToScript,

this.m_cmnuListViewSeparator1,

this.m_cmnuListViewLargeIcons,

this.m_cmnuListViewSmallIcons,

this.m_cmnuListViewDetails,

this.m_cmnuListViewList,

this.m_cmnuListViewSeparator2 } );

this.m_UserContextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuListViewBrowseToScript,

this.m_cmnuListViewSeparator1,

this.m_cmnuListViewLargeIcons,

this.m_cmnuListViewSmallIcons,

this.m_cmnuListViewDetails,

this.m_cmnuListViewList,

this.m_cmnuListViewSeparator2 } );

Debug.WriteLine("Post-initialize check on Default");

foreach (MenuItem mi in this.m_DefaultContextMenu.MenuItems) {

Debug.WriteLine("MenuItem = " + mi.Text);

}

Debug.WriteLine("Post-initialize check on User:");

foreach (MenuItem mi in this.m_UserContextMenu.MenuItems) {

Debug.WriteLine("MenuItem = " + mi.Text);

}

Nov 17 '05 #1
4 1600
When a menuitem from one context menu is added to another context menu, the
..Net framework removes it from the first menu. This is by design. It doesn't
make sense for a menuitem to be present to 2 different menus.

-Atul
http://www.ssware.com/
Shell MegaPack - Windows Explorer Shell Controls for ActiveX and .Net

"ssg31415926" <ne**********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I've got a ListView which can show many different types of object. I
need to display a ContextMenu whose MenuItems depend on the object
type. I was planning to pre-create the ContextMenus when the app is
started and then assign them when the object is clicked on. The first
few items in the menus
will all be the same (Large Icons, Small Icons, etc.).

I create all of those objects, then add them to the first context menu
and all is well. Then I add them to the second context menu and the
first one has zero items. I don't understand why. Why can't a
MenuItem appear on two different ContextMenus?

Here is part of my code. The first foreach loop returns no items. The
second returns the list of MenuItems I'd expect. Can anyone explain
why and how I avoid this. I didn't want to create specific instances
of each MenuItem for each context menu because I wanted, for example,
to store the currently selected View (Details, Small Icons, etc.) in
the appropriate MenuItem.

private ContextMenu m_DefaultContextMenu;

private ContextMenu m_ComputerContextMenu;

private ContextMenu m_UserContextMenu;

private ContextMenu m_GroupContextMenu;

private MenuItem m_cmnuListViewLargeIcons;

private MenuItem m_cmnuListViewSmallIcons;

private MenuItem m_cmnuListViewDetails;

private MenuItem m_cmnuListViewList;

private MenuItem m_cmnuListViewSeparator1;

private MenuItem m_cmnuListViewBrowseToScript;

private MenuItem m_cmnuListViewSeparator2;

this.m_cmnuListViewBrowseToScript = new MenuItem("&Browse to
script...");

this.m_cmnuListViewBrowseToScript.Index = 0;
this.m_cmnuListViewSeparator1 = new MenuItem("-");

this.m_cmnuListViewSeparator1.Index = 1;

this.m_cmnuListViewLargeIcons = new MenuItem("Lar&ge Icons", new
System.EventHandler(this.mnuViewLargeIcons_Click)) ;

this.m_cmnuListViewLargeIcons.Index = 2;

this.m_cmnuListViewSmallIcons = new MenuItem("S&mall Icons", new
System.EventHandler(this.mnuViewSmallIcons_Click)) ;

this.m_cmnuListViewSmallIcons.Index = 3;

this.m_cmnuListViewDetails = new MenuItem("&Details", new
System.EventHandler(this.mnuViewDetails_Click));

this.m_cmnuListViewDetails.Index = 4;

this.m_cmnuListViewList = new MenuItem("&List", new
System.EventHandler(this.mnuViewList_Click));

this.m_cmnuListViewList.Index = 5;

this.m_cmnuListViewSeparator2 = new MenuItem("-");

this.m_cmnuListViewSeparator2.Index = 6;
this.m_cmnuListViewDetails.Checked = true;
this.m_DefaultContextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuListViewBrowseToScript,

this.m_cmnuListViewSeparator1,

this.m_cmnuListViewLargeIcons,

this.m_cmnuListViewSmallIcons,

this.m_cmnuListViewDetails,

this.m_cmnuListViewList,

this.m_cmnuListViewSeparator2 } );

this.m_UserContextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuListViewBrowseToScript,

this.m_cmnuListViewSeparator1,

this.m_cmnuListViewLargeIcons,

this.m_cmnuListViewSmallIcons,

this.m_cmnuListViewDetails,

this.m_cmnuListViewList,

this.m_cmnuListViewSeparator2 } );

Debug.WriteLine("Post-initialize check on Default");

foreach (MenuItem mi in this.m_DefaultContextMenu.MenuItems) {

Debug.WriteLine("MenuItem = " + mi.Text);

}

Debug.WriteLine("Post-initialize check on User:");

foreach (MenuItem mi in this.m_UserContextMenu.MenuItems) {

Debug.WriteLine("MenuItem = " + mi.Text);

}

Nov 17 '05 #2
When a menuitem from one context menu is added to another context menu, the
..Net framework removes it from the first menu. This is by design. It doesn't
make sense for a menuitem to be present to 2 different menus.

-Atul
http://www.ssware.com/
Shell MegaPack - Windows Explorer Shell Controls for ActiveX and .Net

"ssg31415926" <ne**********@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I've got a ListView which can show many different types of object. I
need to display a ContextMenu whose MenuItems depend on the object
type. I was planning to pre-create the ContextMenus when the app is
started and then assign them when the object is clicked on. The first
few items in the menus
will all be the same (Large Icons, Small Icons, etc.).

I create all of those objects, then add them to the first context menu
and all is well. Then I add them to the second context menu and the
first one has zero items. I don't understand why. Why can't a
MenuItem appear on two different ContextMenus?

Here is part of my code. The first foreach loop returns no items. The
second returns the list of MenuItems I'd expect. Can anyone explain
why and how I avoid this. I didn't want to create specific instances
of each MenuItem for each context menu because I wanted, for example,
to store the currently selected View (Details, Small Icons, etc.) in
the appropriate MenuItem.

private ContextMenu m_DefaultContextMenu;

private ContextMenu m_ComputerContextMenu;

private ContextMenu m_UserContextMenu;

private ContextMenu m_GroupContextMenu;

private MenuItem m_cmnuListViewLargeIcons;

private MenuItem m_cmnuListViewSmallIcons;

private MenuItem m_cmnuListViewDetails;

private MenuItem m_cmnuListViewList;

private MenuItem m_cmnuListViewSeparator1;

private MenuItem m_cmnuListViewBrowseToScript;

private MenuItem m_cmnuListViewSeparator2;

this.m_cmnuListViewBrowseToScript = new MenuItem("&Browse to
script...");

this.m_cmnuListViewBrowseToScript.Index = 0;
this.m_cmnuListViewSeparator1 = new MenuItem("-");

this.m_cmnuListViewSeparator1.Index = 1;

this.m_cmnuListViewLargeIcons = new MenuItem("Lar&ge Icons", new
System.EventHandler(this.mnuViewLargeIcons_Click)) ;

this.m_cmnuListViewLargeIcons.Index = 2;

this.m_cmnuListViewSmallIcons = new MenuItem("S&mall Icons", new
System.EventHandler(this.mnuViewSmallIcons_Click)) ;

this.m_cmnuListViewSmallIcons.Index = 3;

this.m_cmnuListViewDetails = new MenuItem("&Details", new
System.EventHandler(this.mnuViewDetails_Click));

this.m_cmnuListViewDetails.Index = 4;

this.m_cmnuListViewList = new MenuItem("&List", new
System.EventHandler(this.mnuViewList_Click));

this.m_cmnuListViewList.Index = 5;

this.m_cmnuListViewSeparator2 = new MenuItem("-");

this.m_cmnuListViewSeparator2.Index = 6;
this.m_cmnuListViewDetails.Checked = true;
this.m_DefaultContextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuListViewBrowseToScript,

this.m_cmnuListViewSeparator1,

this.m_cmnuListViewLargeIcons,

this.m_cmnuListViewSmallIcons,

this.m_cmnuListViewDetails,

this.m_cmnuListViewList,

this.m_cmnuListViewSeparator2 } );

this.m_UserContextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuListViewBrowseToScript,

this.m_cmnuListViewSeparator1,

this.m_cmnuListViewLargeIcons,

this.m_cmnuListViewSmallIcons,

this.m_cmnuListViewDetails,

this.m_cmnuListViewList,

this.m_cmnuListViewSeparator2 } );

Debug.WriteLine("Post-initialize check on Default");

foreach (MenuItem mi in this.m_DefaultContextMenu.MenuItems) {

Debug.WriteLine("MenuItem = " + mi.Text);

}

Debug.WriteLine("Post-initialize check on User:");

foreach (MenuItem mi in this.m_UserContextMenu.MenuItems) {

Debug.WriteLine("MenuItem = " + mi.Text);

}

Nov 17 '05 #3
Thanks for your help. It makes sense if you want to pre-create your
menus and half of the items on different menus are common to each.

There are different types of object that the user can right-click on.
Each type of object will have different functions. In order to have
granularity of control of what a user can do, I'm going to have to test
whether they are allowed perform each function before adding the
relevant MenuItem to the ContextMenu. Because I expect there'll be a
lot of functions, I thought it'd be quicker to create separate
ContextMenus for each object type when the app is started and then
assign the appropriate ContextMenu when the user performs the
right-click. I was trying to avoid having 20 sets of Details, List,
Large Icons, Small Icons, Properties (and so on) MenuItems, plus all
the code to keep the Checked flags and such like in line.

Does this sound like a bad idea? More to the point, is there a better
way?

Nov 17 '05 #4
Thanks for your help. It makes sense if you want to pre-create your
menus and half of the items on different menus are common to each.

There are different types of object that the user can right-click on.
Each type of object will have different functions. In order to have
granularity of control of what a user can do, I'm going to have to test
whether they are allowed perform each function before adding the
relevant MenuItem to the ContextMenu. Because I expect there'll be a
lot of functions, I thought it'd be quicker to create separate
ContextMenus for each object type when the app is started and then
assign the appropriate ContextMenu when the user performs the
right-click. I was trying to avoid having 20 sets of Details, List,
Large Icons, Small Icons, Properties (and so on) MenuItems, plus all
the code to keep the Checked flags and such like in line.

Does this sound like a bad idea? More to the point, is there a better
way?

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Eddie de Bear | last post: by
2 posts views Thread by Patryk | last post: by
1 post views Thread by Doug Bell | last post: by
reply views Thread by ssg31415926 | last post: by
2 posts views Thread by Dino M. Buljubasic | last post: by
reply views Thread by Robert Smith | last post: by
2 posts views Thread by Ron M. Newman | last post: by
reply views Thread by leo001 | last post: by

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.