1

When a user logs into my application, there are some menu items that I don't want every user to see. So I would like to either disable or make invisible the menu item. For example fileToolStripMenuItem is the first item in my menuStrip, when I try:

fileToolStripMenuItem.Enabled = false; - this does not work menuStrip.Items[0].Enabled = false; - this does work

Can anyone enlighten me as to the difference here?

Also, I would like to be able to disable a drop down item from one of the menu items, but I cannot do that either.

Here's the code:

public Form1()
        {
            InitializeComponent();

            // bunch of other code here

            if (!login.ValidatedUser)
            {
                menuStrip1.Items[0].Visible = false; // this works
                toolsToolStripMenuItem.Visible = false; // this does not
                btnStartResourceManager.Enabled = false;
                listAvailableSizes.Enabled = true;
                panelPicSet.Enabled = true;
            }
        }
Nick
  • 1,903
  • 2
  • 21
  • 40

3 Answers3

7

fileToolStripMenuItem.Enabled = false; works as expected. I think you trying to disable it before InitializeComponent(); call.

public form()
{
    InitializeComponent();
    fileToolStripMenuItem.Enabled = false;//disables all file menu
    saveasToolStripMenuItem.Enabled = false; //disables save as menu item in file menu list
}
Renatas M.
  • 11,694
  • 1
  • 43
  • 62
  • Ok I see, but still something is wrong in your code. Can you add code from designer generated code how menustrip and its items is initialized? Which version of framework you using? – Renatas M. Jul 27 '11 at 14:14
  • .NET framework is version 4.0.30319 will post designer code in question – Nick Jul 27 '11 at 14:18
  • Wow. I'm glad you had me look at the designer code. Apparently I did something goofy awhile back and somehow made ghost copies of all of the menu items appended with 1's at the end. for example `fileToolStripMenuItem1` instead of `fileToolStripMenuItem`. So now that I am referencing the right item, all is well. I can't believe I wasted an hour on this... thanks though. – Nick Jul 27 '11 at 14:29
  • :) yes I know, that sometimes problem solution is much simpler than it looks like. – Renatas M. Jul 28 '11 at 05:39
0

For Sub Items, just right click on the item and see its name In Design Section in Properties Window. In my case below addNewToolStripMenuItem1.

public Form()   
    {
        InitializeComponent();
        menuStrip1.Items[1].Visible = false; // For Main Item // Bold Letters
        addNewToolStripMenuItem1.Visible = false; //For Sub Items         
    }
siekfried
  • 2,964
  • 1
  • 21
  • 36
0

Use the specific name of your menu item and change its Visible property. i.e.

 private void toggleToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (shown)
            saveToolStripMenuItem.Visible = false;
        else
            saveToolStripMenuItem.Visible = true;
        shown = !shown;
    }
Joshua Smith
  • 421
  • 1
  • 3
  • 11
  • Thanks for the reply, but as I said in my post, that does not work. It only works if I were to say `menuStrip.Items[0].Visible = false;` I need to know why and how to work around that. – Nick Jul 27 '11 at 13:57