?
Solved

500pts: Get a reference to a menuitem in ContextMenu (winforms)

Posted on 2007-04-05
8
Medium Priority
?
477 Views
Last Modified: 2012-05-05
Hi there,

Can anyone help, i am trying to get a reference to a menuitem in ContextMenu_Popup, currently i am using the index which is really bad as the index can change.. like this..

ContextMenu.MenuItems[11].Enabled = false;

I have added the menuitem like so but i don't see an NAME item or anything that i can replace the index with... Any ideas?

                  MenuItem inserter =new MenuItem();
                  inserter.Shortcut = Shortcut.Ins;
                  inserter.ShowShortcut = true;
                  inserter.Text = "Insert";
                  inserter.Click += new EventHandler(inserter_Click);
                  
                  viewer.ContextMenu.MenuItems.Add(4,inserter);

Thanks in advance

ian
0
Comment
Question by:ianinspain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 16

Expert Comment

by:AlexNek
ID: 18858941
Why not create "inserter" as class data member?
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18859959
You wanted this?


                  MenuItem inserter = new MenuItem();
                  inserter.Shortcut = Shortcut.Ins;
                  inserter.ShowShortcut = true;
                  inserter.Text = "Insert";
//Name is the Key
                  inserter.Name = "ins";
                  inserter.Click += new EventHandler(inserter_Click);

                  ContextMenu ctx = new ContextMenu();
                  viewer.ContextMenu = ctx;  //            0 = index
                  viewer.ContextMenu.MenuItems.Add(0, inserter);
                  viewer.ContextMenu.MenuItems[0].Enabled = false;
                  viewer.ContextMenu.MenuItems["ins"].Enabled = true;
                  MessageBox.Show(viewer.ContextMenu.MenuItems["ins"].Name);
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18859979
Sorry, forgot to explain the bottom half.

viewer.ContextMenu.MenuItems[] has two methods of reference
int i
string key
both for index.   The "key" is the name of the menuItem.

I just ran and compiled this on VS 2005, .NET 2.0 and it worked.  I hope you are using that, else I'm not 100% sure if it will work with 2003.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Accepted Solution

by:
Yttribium earned 1000 total points
ID: 18860048
Okay, I just checked on 2003 on my old system, there is no name attribute, however.  You can still refer to your context menu items by name, if you don't destroy the object that creates them.
i.e.

In your main form, declare your menuItems as global to the form e.g.

private MenuItem inserter = new MenuItem();

//Then this is what I used to test with:
                                           private void Form1_Load(object sender, System.EventArgs e)
            {
                  inserter.Shortcut = Shortcut.Ins;
                  inserter.ShowShortcut = true;
                  inserter.Text = "Insert";
                  inserter.Click +=new EventHandler(inserter_Click);
                  listView1.ContextMenu = contextMenu1;
                  listView1.ContextMenu.MenuItems.Add(0,inserter);
            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                                                               //changing this, it changes the state.
                  inserter.Enabled = !inserter.Enabled;
            }
0
 
LVL 16

Assisted Solution

by:AlexNek
AlexNek earned 1000 total points
ID: 18860185
>In your main form, declare your menuItems as global to the form
is is nor the same as
<create "inserter" as class data member>
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18860279
I totally misunderstood your hint, I was thinking in 2.0, so that didn't appear as a solution to the problem.  Only after I wrote the 2.0 solution I realised he meant 1.1, so I never read your post after that.
 My apologies about this, though, elaborating your idea wouldn't have hurt =/  I wouldn't have glanced over it so fast.  
 
0
 
LVL 16

Expert Comment

by:AlexNek
ID: 18860339
Don't worry, it was more question as hint.
0
 

Author Comment

by:ianinspain
ID: 18888463
Thanks both! .... I was hoping there was a NAME property that i was missing in VS 2003 ... but i suppose i will keep a reference... going to split the points...

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question