Windows Forms - ToolStripItem Visible property is always set to false

Leron_says_get_back_Monica picture Leron_says_get_back_Monica · Mar 28, 2013 · Viewed 9k times · Source

I'm working on a MDI Windows Forms application. My parent form has ToolStrip menu and some 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.

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. However when I get into debug mode in the part foreach (ToolStripItem tempItem in item) the tempItem Visible property is marked as false. In the designer however this property is set to true. You can see that I even try explicitly to change the value to 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.

This is just the begining of the method and I can't think of other code that can mess up with the ToolStrip items. 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. So any idea why this is happening, why I cant change the Visible value and of course any way to do it.

Answer

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. It is designed exactly for this purpose. Hope someone gonna spend less time dealing with this problem by reading this!