问题
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;
}
}
回答1:
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
}
回答2:
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:
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
}
来源:https://stackoverflow.com/questions/6845269/disabling-a-toolstripmenuitem-vs-disabling-a-menustrip-item