Set up context menus in advance

S

ssg31415926

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);

}
 
A

Atul

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 said:
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);

}
 
S

ssg31415926

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?
 

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