[英]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
是我的第一个项目menuStrip
,当我尝试:
fileToolStripMenuItem.Enabled = false;
- this does not work menuStrip.Items[0].Enabled = false;
-这不起作用
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;
}
}
fileToolStripMenuItem.Enabled = false;
works as expected. 如预期般运作。 I think you trying to disable it before InitializeComponent();
我认为您尝试在InitializeComponent()之前禁用它; call.
呼叫。
public form()
{
InitializeComponent();
fileToolStripMenuItem.Enabled = false;//disables all file menu
saveasToolStripMenuItem.Enabled = false; //disables save as menu item in file menu list
}
For Sub Items, just right click on the item and see its name In Design Section in Properties Window. 对于子项目,只需右键单击该项目,然后在“属性”窗口的“设计”部分中查看其名称。 In my case below
addNewToolStripMenuItem1
. 在我的情况下,下面是
addNewToolStripMenuItem1
。
public Form()
{
InitializeComponent();
menuStrip1.Items[1].Visible = false; // For Main Item // Bold Letters
addNewToolStripMenuItem1.Visible = false; //For Sub Items
}
Use the specific name of your menu item and change its Visible property. 使用菜单项的特定名称并更改其Visible属性。 ie
即
private void toggleToolStripMenuItem_Click(object sender, EventArgs e)
{
if (shown)
saveToolStripMenuItem.Visible = false;
else
saveToolStripMenuItem.Visible = true;
shown = !shown;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.