473,325 Members | 2,860 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,325 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 1668
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.