473,727 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_DefaultContex tMenu;

private ContextMenu m_ComputerConte xtMenu;

private ContextMenu m_UserContextMe nu;

private ContextMenu m_GroupContextM enu;

private MenuItem m_cmnuListViewL argeIcons;

private MenuItem m_cmnuListViewS mallIcons;

private MenuItem m_cmnuListViewD etails;

private MenuItem m_cmnuListViewL ist;

private MenuItem m_cmnuListViewS eparator1;

private MenuItem m_cmnuListViewB rowseToScript;

private MenuItem m_cmnuListViewS eparator2;

this.m_cmnuList ViewBrowseToScr ipt = new MenuItem("&Brow se to
script...");

this.m_cmnuList ViewBrowseToScr ipt.Index = 0;
this.m_cmnuList ViewSeparator1 = new MenuItem("-");

this.m_cmnuList ViewSeparator1. Index = 1;

this.m_cmnuList ViewLargeIcons = new MenuItem("Lar&g e Icons", new
System.EventHan dler(this.mnuVi ewLargeIcons_Cl ick));

this.m_cmnuList ViewLargeIcons. Index = 2;

this.m_cmnuList ViewSmallIcons = new MenuItem("S&mal l Icons", new
System.EventHan dler(this.mnuVi ewSmallIcons_Cl ick));

this.m_cmnuList ViewSmallIcons. Index = 3;

this.m_cmnuList ViewDetails = new MenuItem("&Deta ils", new
System.EventHan dler(this.mnuVi ewDetails_Click ));

this.m_cmnuList ViewDetails.Ind ex = 4;

this.m_cmnuList ViewList = new MenuItem("&List ", new
System.EventHan dler(this.mnuVi ewList_Click));

this.m_cmnuList ViewList.Index = 5;

this.m_cmnuList ViewSeparator2 = new MenuItem("-");

this.m_cmnuList ViewSeparator2. Index = 6;
this.m_cmnuList ViewDetails.Che cked = true;
this.m_DefaultC ontextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuList ViewBrowseToScr ipt,

this.m_cmnuList ViewSeparator1,

this.m_cmnuList ViewLargeIcons,

this.m_cmnuList ViewSmallIcons,

this.m_cmnuList ViewDetails,

this.m_cmnuList ViewList,

this.m_cmnuList ViewSeparator2 } );

this.m_UserCont extMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuList ViewBrowseToScr ipt,

this.m_cmnuList ViewSeparator1,

this.m_cmnuList ViewLargeIcons,

this.m_cmnuList ViewSmallIcons,

this.m_cmnuList ViewDetails,

this.m_cmnuList ViewList,

this.m_cmnuList ViewSeparator2 } );

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

foreach (MenuItem mi in this.m_DefaultC ontextMenu.Menu Items) {

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

}

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

foreach (MenuItem mi in this.m_UserCont extMenu.MenuIte ms) {

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

}

Nov 17 '05 #1
4 1715
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

"ssg3141592 6" <ne**********@g mail.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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_DefaultContex tMenu;

private ContextMenu m_ComputerConte xtMenu;

private ContextMenu m_UserContextMe nu;

private ContextMenu m_GroupContextM enu;

private MenuItem m_cmnuListViewL argeIcons;

private MenuItem m_cmnuListViewS mallIcons;

private MenuItem m_cmnuListViewD etails;

private MenuItem m_cmnuListViewL ist;

private MenuItem m_cmnuListViewS eparator1;

private MenuItem m_cmnuListViewB rowseToScript;

private MenuItem m_cmnuListViewS eparator2;

this.m_cmnuList ViewBrowseToScr ipt = new MenuItem("&Brow se to
script...");

this.m_cmnuList ViewBrowseToScr ipt.Index = 0;
this.m_cmnuList ViewSeparator1 = new MenuItem("-");

this.m_cmnuList ViewSeparator1. Index = 1;

this.m_cmnuList ViewLargeIcons = new MenuItem("Lar&g e Icons", new
System.EventHan dler(this.mnuVi ewLargeIcons_Cl ick));

this.m_cmnuList ViewLargeIcons. Index = 2;

this.m_cmnuList ViewSmallIcons = new MenuItem("S&mal l Icons", new
System.EventHan dler(this.mnuVi ewSmallIcons_Cl ick));

this.m_cmnuList ViewSmallIcons. Index = 3;

this.m_cmnuList ViewDetails = new MenuItem("&Deta ils", new
System.EventHan dler(this.mnuVi ewDetails_Click ));

this.m_cmnuList ViewDetails.Ind ex = 4;

this.m_cmnuList ViewList = new MenuItem("&List ", new
System.EventHan dler(this.mnuVi ewList_Click));

this.m_cmnuList ViewList.Index = 5;

this.m_cmnuList ViewSeparator2 = new MenuItem("-");

this.m_cmnuList ViewSeparator2. Index = 6;
this.m_cmnuList ViewDetails.Che cked = true;
this.m_DefaultC ontextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuList ViewBrowseToScr ipt,

this.m_cmnuList ViewSeparator1,

this.m_cmnuList ViewLargeIcons,

this.m_cmnuList ViewSmallIcons,

this.m_cmnuList ViewDetails,

this.m_cmnuList ViewList,

this.m_cmnuList ViewSeparator2 } );

this.m_UserCont extMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuList ViewBrowseToScr ipt,

this.m_cmnuList ViewSeparator1,

this.m_cmnuList ViewLargeIcons,

this.m_cmnuList ViewSmallIcons,

this.m_cmnuList ViewDetails,

this.m_cmnuList ViewList,

this.m_cmnuList ViewSeparator2 } );

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

foreach (MenuItem mi in this.m_DefaultC ontextMenu.Menu Items) {

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

}

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

foreach (MenuItem mi in this.m_UserCont extMenu.MenuIte ms) {

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

"ssg3141592 6" <ne**********@g mail.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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_DefaultContex tMenu;

private ContextMenu m_ComputerConte xtMenu;

private ContextMenu m_UserContextMe nu;

private ContextMenu m_GroupContextM enu;

private MenuItem m_cmnuListViewL argeIcons;

private MenuItem m_cmnuListViewS mallIcons;

private MenuItem m_cmnuListViewD etails;

private MenuItem m_cmnuListViewL ist;

private MenuItem m_cmnuListViewS eparator1;

private MenuItem m_cmnuListViewB rowseToScript;

private MenuItem m_cmnuListViewS eparator2;

this.m_cmnuList ViewBrowseToScr ipt = new MenuItem("&Brow se to
script...");

this.m_cmnuList ViewBrowseToScr ipt.Index = 0;
this.m_cmnuList ViewSeparator1 = new MenuItem("-");

this.m_cmnuList ViewSeparator1. Index = 1;

this.m_cmnuList ViewLargeIcons = new MenuItem("Lar&g e Icons", new
System.EventHan dler(this.mnuVi ewLargeIcons_Cl ick));

this.m_cmnuList ViewLargeIcons. Index = 2;

this.m_cmnuList ViewSmallIcons = new MenuItem("S&mal l Icons", new
System.EventHan dler(this.mnuVi ewSmallIcons_Cl ick));

this.m_cmnuList ViewSmallIcons. Index = 3;

this.m_cmnuList ViewDetails = new MenuItem("&Deta ils", new
System.EventHan dler(this.mnuVi ewDetails_Click ));

this.m_cmnuList ViewDetails.Ind ex = 4;

this.m_cmnuList ViewList = new MenuItem("&List ", new
System.EventHan dler(this.mnuVi ewList_Click));

this.m_cmnuList ViewList.Index = 5;

this.m_cmnuList ViewSeparator2 = new MenuItem("-");

this.m_cmnuList ViewSeparator2. Index = 6;
this.m_cmnuList ViewDetails.Che cked = true;
this.m_DefaultC ontextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuList ViewBrowseToScr ipt,

this.m_cmnuList ViewSeparator1,

this.m_cmnuList ViewLargeIcons,

this.m_cmnuList ViewSmallIcons,

this.m_cmnuList ViewDetails,

this.m_cmnuList ViewList,

this.m_cmnuList ViewSeparator2 } );

this.m_UserCont extMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuList ViewBrowseToScr ipt,

this.m_cmnuList ViewSeparator1,

this.m_cmnuList ViewLargeIcons,

this.m_cmnuList ViewSmallIcons,

this.m_cmnuList ViewDetails,

this.m_cmnuList ViewList,

this.m_cmnuList ViewSeparator2 } );

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

foreach (MenuItem mi in this.m_DefaultC ontextMenu.Menu Items) {

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

}

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

foreach (MenuItem mi in this.m_UserCont extMenu.MenuIte ms) {

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
4777
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 creation takes some time. The approach I took was to override the OnSelect method of the MenuItem class, which had code to populate the
2
1665
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 menus but the main problem is that they have to be downloaded from server after the page loads. I can't load all of them on page because there are to many. This is my solution of the problem. When user right-clicks js download from server through...
1
1549
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 the Explorer Windows opens the context menu with the option "Use My App" and selecting this runs My App with the selected file name. Just as I wanted. The problem I now have is that double clicking a Visual Studio file (.sln) also starts My...
0
324
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 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...
2
1640
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 me to chese between editing or deleting the item from the list view. That works fine. However, in addition to calling the context menu on Click, I can call it also by RightClick which causes application to either freeze, crash, call Delete...
0
1027
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. Ideas ideas how you would do this. Thanx in advance Robert
2
3451
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 event to I "capture" in order to build the tree node menu in real time? right click on a tree node? or is it too late? just FYI: the menu is different for each node and is based on "real time"
2
3175
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
1478
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 advance for any info
0
8891
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9406
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9260
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8103
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6703
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.