c# - 如何设置 ToolStripMenuItem 在代码中可见?

标签 c# winforms toolstripdropdown

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

这是我的代码:

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是一个复杂的属性。它的设置和读取不同。

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

因此设置和读取它是另一回事:即使您将其设置为 true , 它可能会来 false当你读回它时在调试器中(同样,如果链中的任何父级被隐藏):它将变成 true但是当所有的 parent 都可见时。

对于 ToolStripItem具体来说,使用 Available 属性而不是 Visible : 这应该可以满足您的期望。文档(我已链接)专门讨论了这一点:

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.

关于c# - 如何设置 ToolStripMenuItem 在代码中可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38177385/

相关文章:

c# - 如何使用 LINQ 将 List<string> 中的所有字符串转换为小写?

c# - 底层表单消失但仅当对话框显示时

c# - ToolStripDropDownButton,如何删除所有 DropDownItems?

c# - 当鼠标在控件上方时如何消除 ToolStripDropDownButton 上的蓝色焦点

c# - 如何提高.NET Windows应用程序中保存多行的效率?

c# - "being moved to page file"事件

winforms - 如何更改 BarButtonItem 的背景颜色?

c# - 调整控件大小时 Winforms 自动滚动丢失

c# - 窗口失去焦点后更新 ToolstripDropdownButton 时出现问题

C# 如何确保所选节点在失去焦点时保持突出显示