Disabling a ToolStripMenuItem vs. disabling a Menu

2019-06-08 18:24发布

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

3条回答
我只想做你的唯一
2楼-- · 2019-06-08 18:55

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;
    }
查看更多
走好不送
3楼-- · 2019-06-08 19:03

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         
    }
查看更多
仙女界的扛把子
4楼-- · 2019-06-08 19:04

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
}
查看更多
登录 后发表回答