ToolStripMenuItem.Visible always returns false? (Clarified hopefully)
-
I have just struck this. Using VS2008
private void button2_Click(object sender, EventArgs e)
{// ToolStripMenuItem.Visible always returns false? // This has the effect of not being able to toggle or invert the state to false by // assigning its negated value by use of the ! operator. // If the initial state is Visible = false the first click will toggle to Visible = true // Subsequent clicks do not change the Visible property as Visible always returns false // So, no matter the true state it will always set Visible = true rather than the inverse; // always returns false even if set to Visible = True bool cond1 = this.confirmDeletesToolStripMenuItem.Visible; // always returns true bool cond2 = !this.confirmDeletesToolStripMenuItem.Visible; MessageBox.Show(string.Format("Cond1 = {0} Cond2 = {1}",cond1,cond2)); // try to invert the value of confirmDeletesToolStripMenuItem.Visible this.confirmDeletesToolStripMenuItem.Visible = !this.confirmDeletesToolStripMenuItem.Visible; // still always returns false cond1 = this.confirmDeletesToolStripMenuItem.Visible; // still always returns true cond2 = !this.confirmDeletesToolStripMenuItem.Visible; MessageBox.Show(string.Format("Cond1 = {0} Cond2 = {1}", cond1, cond2)); }
The confirmDeletesToolStripMenuItem is not the topmost menuitem. If I get the value of any top level menu item it returns the correct value i.e True if set to Visible = True; Whereas the lower menu levels return False even if set to True. I would have thought that the Visible property is a reflection of whether it should be displayed rather than whether it can be seen? I can set the Visible property directly eg
this.confirmDeletesToolStripMenuItem.Visible = True;
but still returns False. Thanks for any help.... -
I have just struck this. Using VS2008
private void button2_Click(object sender, EventArgs e)
{// ToolStripMenuItem.Visible always returns false? // This has the effect of not being able to toggle or invert the state to false by // assigning its negated value by use of the ! operator. // If the initial state is Visible = false the first click will toggle to Visible = true // Subsequent clicks do not change the Visible property as Visible always returns false // So, no matter the true state it will always set Visible = true rather than the inverse; // always returns false even if set to Visible = True bool cond1 = this.confirmDeletesToolStripMenuItem.Visible; // always returns true bool cond2 = !this.confirmDeletesToolStripMenuItem.Visible; MessageBox.Show(string.Format("Cond1 = {0} Cond2 = {1}",cond1,cond2)); // try to invert the value of confirmDeletesToolStripMenuItem.Visible this.confirmDeletesToolStripMenuItem.Visible = !this.confirmDeletesToolStripMenuItem.Visible; // still always returns false cond1 = this.confirmDeletesToolStripMenuItem.Visible; // still always returns true cond2 = !this.confirmDeletesToolStripMenuItem.Visible; MessageBox.Show(string.Format("Cond1 = {0} Cond2 = {1}", cond1, cond2)); }
The confirmDeletesToolStripMenuItem is not the topmost menuitem. If I get the value of any top level menu item it returns the correct value i.e True if set to Visible = True; Whereas the lower menu levels return False even if set to True. I would have thought that the Visible property is a reflection of whether it should be displayed rather than whether it can be seen? I can set the Visible property directly eg
this.confirmDeletesToolStripMenuItem.Visible = True;
but still returns False. Thanks for any help....So, if the item is visible and you then do 'item.Visible = false;' does that make it disappear? And how about after 'Visible' is set to 'false'? If you then call 'item.Visible = true;' does the item actually show up again? Just to make sure I'm clear, I don't mean "does that return true", I mean does it physically disappear/appear as it should? That is unlike anything I've ever seen. I have a project that I'm working on in which I toggle the 'Visible' property for several menu and toolstrip items. Each of them works perfectly normal.
-
So, if the item is visible and you then do 'item.Visible = false;' does that make it disappear? And how about after 'Visible' is set to 'false'? If you then call 'item.Visible = true;' does the item actually show up again? Just to make sure I'm clear, I don't mean "does that return true", I mean does it physically disappear/appear as it should? That is unlike anything I've ever seen. I have a project that I'm working on in which I toggle the 'Visible' property for several menu and toolstrip items. Each of them works perfectly normal.
Matt U. wrote:
So, if the item is visible and you then do 'item.Visible = false;' does that make it disappear? And how about after 'Visible' is set to 'false'? If you then call 'item.Visible = true;' does the item actually show up again?
Yes when setting the Visible property in this way the item disappears and appears as you would expect. It just always returns false. Even something like....
item.Visible = true;
bool isVisible = item.Visible;returns false, despite actually being Visible. Hence the inabilty to Toggle based on it current state. What I have found is that the top level menu items behave as expected, whereas the lower level, or at least mine on the 2nd and 3rd levels don't seem to behave?
modified on Friday, September 3, 2010 12:59 AM
-
I have just struck this. Using VS2008
private void button2_Click(object sender, EventArgs e)
{// ToolStripMenuItem.Visible always returns false? // This has the effect of not being able to toggle or invert the state to false by // assigning its negated value by use of the ! operator. // If the initial state is Visible = false the first click will toggle to Visible = true // Subsequent clicks do not change the Visible property as Visible always returns false // So, no matter the true state it will always set Visible = true rather than the inverse; // always returns false even if set to Visible = True bool cond1 = this.confirmDeletesToolStripMenuItem.Visible; // always returns true bool cond2 = !this.confirmDeletesToolStripMenuItem.Visible; MessageBox.Show(string.Format("Cond1 = {0} Cond2 = {1}",cond1,cond2)); // try to invert the value of confirmDeletesToolStripMenuItem.Visible this.confirmDeletesToolStripMenuItem.Visible = !this.confirmDeletesToolStripMenuItem.Visible; // still always returns false cond1 = this.confirmDeletesToolStripMenuItem.Visible; // still always returns true cond2 = !this.confirmDeletesToolStripMenuItem.Visible; MessageBox.Show(string.Format("Cond1 = {0} Cond2 = {1}", cond1, cond2)); }
The confirmDeletesToolStripMenuItem is not the topmost menuitem. If I get the value of any top level menu item it returns the correct value i.e True if set to Visible = True; Whereas the lower menu levels return False even if set to True. I would have thought that the Visible property is a reflection of whether it should be displayed rather than whether it can be seen? I can set the Visible property directly eg
this.confirmDeletesToolStripMenuItem.Visible = True;
but still returns False. Thanks for any help....This is the correct behaviour. As the parent menu item isn't visible, it is impossible for the child to be visible therefore setting the
Visible
property has no effect. You should either: 1. Set the visibility of the higher items first. 2. Use theEnabled
property instead. You could iterate over all the items and setVisible
based onEnabled
.private void SetMenuItemsVisiblity(MenuStrip menuStrip) { foreach (ToolStripMenuItem item in menuStrip.Items) { item.Visible = item.Enabled; if (item.Visible && item.DropDownItems.Count > 0) SetToolStripMenuItemVisiblity(item); } } private void SetToolStripMenuItemVisiblity(ToolStripMenuItem item) { foreach (ToolStripMenuItem subItem in item.DropDownItems) { subItem.Visible = subItem.Enabled; if (subItem.Visible && item.DropDownItems.Count > 0) SetToolStripMenuItemVisiblity(subItem); } }
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
This is the correct behaviour. As the parent menu item isn't visible, it is impossible for the child to be visible therefore setting the
Visible
property has no effect. You should either: 1. Set the visibility of the higher items first. 2. Use theEnabled
property instead. You could iterate over all the items and setVisible
based onEnabled
.private void SetMenuItemsVisiblity(MenuStrip menuStrip) { foreach (ToolStripMenuItem item in menuStrip.Items) { item.Visible = item.Enabled; if (item.Visible && item.DropDownItems.Count > 0) SetToolStripMenuItemVisiblity(item); } } private void SetToolStripMenuItemVisiblity(ToolStripMenuItem item) { foreach (ToolStripMenuItem subItem in item.DropDownItems) { subItem.Visible = subItem.Enabled; if (subItem.Visible && item.DropDownItems.Count > 0) SetToolStripMenuItemVisiblity(subItem); } }
Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Thanks Dave
DaveyM69 wrote:
This is the correct behaviour. As the parent menu item isn't visible, it is impossible for the child to be visible therefore setting the Visible property has no effect.
The parent menu item is visible. If I get the value of any top level menu item it returns the correct value i.e True if set to Visible = True; Whereas the lower menu levels return False even if set to True. (Even if the parent menu item is Visible) It looks to me like a subitem returns false because it is not visible until the menu drops down from the parent. Is this normal behaviour? Is the system changing the Visibility property temporarily until the menuitem drops down and is visible? (if the Parent is visible and even if subitem.Visibilty = true) Lew
-
Thanks Dave
DaveyM69 wrote:
This is the correct behaviour. As the parent menu item isn't visible, it is impossible for the child to be visible therefore setting the Visible property has no effect.
The parent menu item is visible. If I get the value of any top level menu item it returns the correct value i.e True if set to Visible = True; Whereas the lower menu levels return False even if set to True. (Even if the parent menu item is Visible) It looks to me like a subitem returns false because it is not visible until the menu drops down from the parent. Is this normal behaviour? Is the system changing the Visibility property temporarily until the menuitem drops down and is visible? (if the Parent is visible and even if subitem.Visibilty = true) Lew
Ah! Now I understand. The
Visible
property returns whether it's actually being displayed right now. This should IMO be read-only in this case as setting it actually sets another property instead! Check out the Available[^] property which behaves exactly as you want.Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Ah! Now I understand. The
Visible
property returns whether it's actually being displayed right now. This should IMO be read-only in this case as setting it actually sets another property instead! Check out the Available[^] property which behaves exactly as you want.Dave
If this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)