Windows Forms - ToolStripItem Visible property is always set to false
Asked Answered
C

2

11

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.

Combust answered 28/3, 2013 at 13:58 Comment(1)
changing "item" to "items" does not harm, and will improve the readability much.Coin
C
25

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!

Combust answered 1/4, 2013 at 11:22 Comment(2)
That's not how it works. Available and Visible are almost identical properties and setting one changes the other. But with one difference, the Visible property only returns true when the user can see the item. Which will never be the case inside your form constructor or Load event, the form isn't yet visible. So high odds that there was not a real problem to being with, you just got confused by what the debugger told you.Morse
The problem was how in runtime to determine what is visible and what not for certain user due to his permissions. If any ToolStripItem is available then the DropDownButton should be visible if there is no ToolStripItem available for the user the DropDownButton should be hidden too. Using Visible property doesn't help here, because as you said it will always return false and my DropDownButton will always be hidden. But if I set the Available property then everything is ok.Combust
A
1

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.

Alard answered 28/3, 2013 at 14:2 Comment(1)
The problem is not that I can't make a list of my ToolStrip items I want the problem is that they have Visible set to false and I can't find a way to change it to true. item.Visible = true leaves the item invisible and in debug mode the value of visible after this row is still false even though I just have set it to true.Combust

© 2022 - 2024 — McMap. All rights reserved.