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.
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!
The following will go trough all toolstripitems
within menuStrip1
:
List<ToolStripMenuItem> allItems = new List<ToolStripMenuItem>();
foreach (ToolStripMenuItem toolItem in menuStrip1.Items)
{
allItems.Add(toolItem);
//add sub items
allItems.AddRange(GetItems(toolItem));
}
foreach (ToolStripMenuItem item in allItems)
{
//make your toolstripMenuItem invisible or whatever you want to do with it.
}
allItems.Clear();
Change menuStrip1
to whatever you call your toolstrip
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With