• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1291
  • Last Modified:

WinForms C#: I need to get a MenuStripItem from a MenuStrip variable

I have the MenuStrip and filled it with items using the designer. Now, from the code, I need to get a refernce to a given menu item.

How do I do this?
0
newbieweb
Asked:
newbieweb
  • 5
  • 4
1 Solution
 
Luis PérezSoftware Architect in .NetCommented:
Have you tried this.menuName?

For example:
this.mnuFile.Text = "File";

Hope that helps.
0
 
newbiewebAuthor Commented:
I need to get the item to enable/disable it.  How do I do that?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The default name is the Text, with the first letter lower-case, followed by "ToolStripMenuItem".

So if the menu said "File" it would be:

    fileToolStripMenuItem.Enabled = false;
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!

 
newbiewebAuthor Commented:
That's neat.  But let's say I wanted to disable File > Save...

and that I do indeed know the Name of the corresponding ToolStripMenuItem.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
?

Have you tried?

    saveToolStripMenuItem.Enabled = false;
0
 
newbiewebAuthor Commented:
Only the parent showed up with Intellisense.

ruleToolStripMenuItem

contains the "Add..." menu item

when I type "a", nothing shows up.  Can I query ruleToolStripMenuItem to find a menu item with a given Name?  That menu item belongs to ruleToolStripMenuItem.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure where you're going wrong.  =\

This works for me:
        private void button1_Click(object sender, EventArgs e)
        {
            addToolStripMenuItem.Enabled = false;
        }

Open in new window


And a picture to show my setup:
Rule --> Add menu setup
0
 
newbiewebAuthor Commented:
That works fine for me, too.  But I do not want to disable the whole menu, just one menu item.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
That will only disable the "Add" menu item.  Everything else would remain available...

Can you explain in more detail about your setup and what is actually happening?
0
 
newbiewebAuthor Commented:
My bad.  I see the items in Intellisense.

Thanks!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now