简体   繁体   English

如何在代码中将visible设置为true?

[英]How to set visible to true in code?

I have some code in Windows Forms application.I want to change the visibility of my drop Down items of toolStripMenuItem in c# code.I set the visibility,but when I set breakpoints in the code the visibility of item doesn't change. 我在Windows窗体应用程序中有一些代码。我想在c#代码中更改toolStripMenuItem的下拉项目的可见性。我设置了可见性,但是当我在代码中设置断点时,项目的可见性不会改变。

Here is my code: 这是我的代码:

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;
       }                                                
   }                                           
}

How to Solve this? 如何解决这个问题?

Visible is a complicated property. Visible是一个复杂的属性。 It doesn't set and read the same. 它没有设置和读取相同的内容。

If you set it to true or false it says whether the object will be (or not) visible. 如果设置truefalse则表示对象是否可见。 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,但如果链中的任何父项也被隐藏,它将显示为false

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. 因此设置和读取它是一回事:即使你将它设置为true ,当你读回它时,调试器中也可能会出现false (同样,如果链中的任何父项被隐藏):它将变为true虽然父母是可见的。

For ToolStripItem specifically though, use the Available property instead of Visible : this should do what you are expecting. 但是对于ToolStripItem ,请使用Available属性而不是Visible :这应该可以满足您的期望。 The documentation (which I linked) talks specifically about this: 文档(我链接)具体谈到这个:

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. Available属性与Visible属性不同,Available表示是否显示ToolStripItem,而Visible表示是否显示ToolStripItem及其父元素。 Setting either Available or Visible to true or false sets the other property to true or false. 将Available或Visible设置为true或false会将other属性设置为true或false。

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

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