473,383 Members | 1,840 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,383 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 1682
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eddie de Bear | last post by:
Hi, A project I am working on has a requirement for dynamic menus. For the most part this works really well. The menus I'm creating a based on files and directories, so naturally the menu...
2
by: Patryk | last post by:
Hi I'm making a web application which is a copy of existing desktop application. I use telerik controlls for MS visual studio I have a little bit complicated situation beacuse I need context...
1
by: Doug Bell | last post by:
Hi, If I modify my Registry adding a new key HKEY_CLASSES_ROOT\*\shell\Use My App\command with a (Default) Value of C:\Program Files\Doug\DotNetApps\MyApp.exe "%1" then right clicking on files in...
0
by: ssg31415926 | last post by:
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...
2
by: Dino M. Buljubasic | last post by:
I have several context menus added to my form. The form is displaying items in listviews connected to the context menus. When I click on an item in a list view, a popup context menu shows allowing...
0
by: Robert Smith | last post by:
Hello, I have written some context menus in my application, however I see lots of nice icons on the menus that microsoft use. It is not obvious how one would add these icons to the menu items....
2
by: Ron M. Newman | last post by:
Hi, Just need a little advice. Id like to build *dynamic* context menus for tree nodes. I'm pretty versed in building context menus and attaching them to tree nodes. My question is, what...
2
by: Keith Hutchison | last post by:
G'day Is it possible to do custom context sensitive menus within MS Access. If yes, does anyone have a sample example? Thanks in advance Keith
2
by: Academia | last post by:
I can't tell for sure but debugging seems to indicate that WM_INITMENU is not raised in WndProc for context menus, only for main menus. Do you happen know if that is true? Thanks in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.