?
Solved

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

Posted on 2007-04-05
8
Medium Priority
?
486 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

830 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