Solved

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

Posted on 2007-04-05
8
471 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
Industry Leaders: 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 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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