Solved

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

Posted on 2007-04-05
8
442 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
 
LVL 5

Accepted Solution

by:
Yttribium earned 250 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 16

Assisted Solution

by:AlexNek
AlexNek earned 250 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now