Solved

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

Posted on 2007-04-05
8
451 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
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…

816 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

12 Experts available now in Live!

Get 1:1 Help Now