当用户登录到我的应用程序,有我不希望每个用户看到的一些菜单项。所以,我想无论是禁用或使无形的菜单项。例如 fileToolStripMenuItem
中的第一个项目我的的MenuStrip
,当我尝试:
fileToolStripMenuItem.Enabled = FALSE;
- 这不起作用
menuStrip.Items [0] .Enabled = FALSE;
- 这确实工作
任何人都可以启发我,这里的区别是什么?
另外,我想应该能够从菜单项禁用下拉项目,但我不能做,要么。
这里的code:
公共Form1中()
{
的InitializeComponent();
//一堆其他的code在这里
如果(!login.ValidatedUser)
{
menuStrip1.Items [0]。可见=虚假的; //这个工程
toolsToolStripMenuItem.Visible = FALSE; //这不
btnStartResourceManager.Enabled = FALSE;
listAvailableSizes.Enabled = TRUE;
panelPicSet.Enabled = TRUE;
}
}
解决方案
fileToolStripMenuItem.Enabled = FALSE;
按预期工作。我想你想的InitializeComponent()之前则禁用;调用。
公开形式()
{
的InitializeComponent();
fileToolStripMenuItem.Enabled = FALSE; //禁用所有的文件菜单
saveasToolStripMenuItem.Enabled = FALSE; //禁用另存为菜单项中的文件菜单列表
}
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;
}
}
解决方案
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
}