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.
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?
Visibleis a complicated property. It doesn't set and read the same.If you set it to
trueorfalseit 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 asfalseif any parent in the chain is also hidden.So setting and reading it is a different thing: even if you set it to
true, it may comefalsein the debugger when you read it back (again, if any parent in the chain is hidden): it'll becometruewhen all the parents are visible though.For
ToolStripItemspecifically though, use theAvailableproperty instead ofVisible: this should do what you are expecting. The documentation (which I linked) talks specifically about this: