简体   繁体   English

Windows窗体 - ToolStripItem可见属性始终设置为false

[英]Windows Forms - ToolStripItem Visible property is always set to false

I'm working on a MDI Windows Forms application. 我正在研究MDI Windows Forms应用程序。 My parent form has ToolStrip menu and some ToolStripDropDownButtons . 我的父窗体有ToolStrip菜单和一些ToolStripDropDownButtons I want to change the Visible property of the ToolStripDropDownButton or to some of the ToolStripItems (sub buttons) that it has accordingly to the permission of the user. 我想根据用户的权限更改ToolStripDropDownButtonVisible属性或某些ToolStripItems (子按钮)。

Here is the part of the method that I've wrote to manage this: 以下是我编写的用于管理此方法的方法的一部分:

private void SetToolStripDropDownVisibility(ToolStripDropDownButton mainBtn, params ToolStripItem[] item)
{
     mainBtn.Visible = false;
     foreach (ToolStripItem tempItem in item)
     {
         tempItem.Visible = true;
     }
}

I'm passing as first argument the ToolStripDropDownButton and all other "sub buttons" as params list. 我作为第一个参数传递ToolStripDropDownButton和所有其他“子按钮”作为参数列表。 However when I get into debug mode in the part foreach (ToolStripItem tempItem in item) the tempItem Visible property is marked as false. 但是当我进入部分foreach (ToolStripItem tempItem in item)的调试模式时, tempItem Visible属性被标记为false。 In the designer however this property is set to true. 但是在设计器中,此属性设置为true。 You can see that I even try explicitly to change the value to true - tempItem.Visible = true; 你可以看到我甚至尝试明确地将值更改为true - tempItem.Visible = true; but it seems as if this line is doing nothing. 但好像这条线似乎无所作为。 The value of Visible remains false and I can't change it. Visible的值仍为false ,我无法更改它。

This is just the begining of the method and I can't think of other code that can mess up with the ToolStrip items. 这只是方法的开头,我想不出其他可能搞乱ToolStrip项目的代码。 I tried to change the value of mainBtn.Visible to true or false thinking that maybe there's any connection but it seems this is not the issues. 我试图将mainBtn.Visible的值mainBtn.Visible为true或false,认为可能存在任何连接,但似乎这不是问题。 So any idea why this is happening, why I cant change the Visible value and of course any way to do it. 所以任何想法为什么会发生这种情况,为什么我无法改变Visible值,当然也没有任何方法可以做到这一点。

The solution is easy and yet not obvious. 解决方案很简单,但并不明显。 When we have to work with ToolStripItems which are part of ToolSTripDropDownButton and solve visibility problem the way we used to solve it with ordinary buttons we have to use Available property. 当我们必须使用属于ToolSTripDropDownButton ToolStripItems并解决可见性问题时,我们必须使用普通按钮来解决它,我们必须使用Available属性。 It is designed exactly for this purpose. 它专为此目的而设计。 Hope someone gonna spend less time dealing with this problem by reading this! 希望有人通过阅读这个来减少处理这个问题的时间!

The following will go trough all toolstripitems within menuStrip1 : 以下内容将通过menuStrip1所有toolstripitems menuStrip1

           List<ToolStripMenuItem> allItems = new List<ToolStripMenuItem>();
            foreach (ToolStripMenuItem toolItem in menuStrip1.Items) 
            {
                allItems.Add(toolItem);
                //add sub items
                allItems.AddRange(GetItems(toolItem));
            }
            foreach (ToolStripMenuItem item in allItems)
            {
                //make your toolstripMenuItem invisible or whatever you want to do with it.
            }
            allItems.Clear();

Change menuStrip1 to whatever you call your toolstrip . menuStrip1更改为您调用toolstrip任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM