如何在代码中设置 ToolStripMenuItem Visible? [英] How to set a ToolStripMenuItem Visible in code?

查看:22
本文介绍了如何在代码中设置 ToolStripMenuItem Visible?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 窗体应用程序中有一些代码.
我想在代码中更改我的下拉 ToolStripMenuItems 的可见性.
我设置了 Visible 属性,但是当我设置断点并检查属性值时,项目的可见性没有改变.

I have some code in Windows Forms application.
I want to change the visibility of my drop down ToolStripMenuItems in code.
I set the Visible property, but when I set a breakpoint and inspect the property value, the visibility of the items has not changed.

这是我的代码:

foreach (ToolStripMenuItem it in _frmMain.menuStripMain.Items)
{
   foreach (ToolStripMenuItem i in it.DropDownItems)
   {
       if (i.Text == this._listAppSchema[0].ObjectName.ToString())
       {
          i.Visible = true;
       }
       else
       {
          i.Visible = false;
       }                                                
   }                                           
}

如何解决这个问题?

推荐答案

Visible 是一个复杂属性.它的设置和读取方式不同.

Visible is a complicated property. It doesn't set and read the same.

如果您将其设置truefalse,则表示该对象是否可见(或不可见).但是,当您读取它时,它会显示该控件的可见性是设置为 true 还是 false,但如果 链中的任何父项,它将读取为 falsestrong> 也是隐藏的.

If you set it to true or false it says whether the object will be (or not) visible. However when you read it, it shows whether that control's visibility is set to true or false, but it will read as false if any parent in the chain is also hidden.

所以设置和读取它是另一回事:即使你将它设置为 true,当你读取它时它可能会在调试器中出现 false(再次,如果链中的任何父节点被隐藏):当所有父节点都可见时,它将变为 true.

So setting and reading it is a different thing: even if you set it to true, it may come false in the debugger when you read it back (again, if any parent in the chain is hidden): it'll become true when all the parents are visible though.

对于 ToolStripItem,特别是使用 Available 属性而不是 Visible:这应该符合您的预期.文档(我链接的)专门讨论了这一点:

For ToolStripItem specifically though, use the Available property instead of Visible: this should do what you are expecting. The documentation (which I linked) talks specifically about this:

Available 属性与 Visible 属性的不同之处在于,Available 表示是否显示 ToolStripItem,而 Visible 表示是否显示 ToolStripItem 及其父项.将 Available 或 Visible 设置为 true 或 false 会将其他属性设置为 true 或 false.

The Available property is different from the Visible property in that Available indicates whether the ToolStripItem is shown, while Visible indicates whether the ToolStripItem and its parent are shown. Setting either Available or Visible to true or false sets the other property to true or false.

这篇关于如何在代码中设置 ToolStripMenuItem Visible?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆