Search code examples
c#.netwinformsmenustrip

Disabling a ToolStripMenuItem vs. disabling a MenuStrip.Item


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

Solution

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