ToolStripMenuItem.Visible always returns false?
-
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 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)); }
Thanks for any clues!
-
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 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)); }
Thanks for any clues!
Hmm? There must be something wrong, but what? I checked your code on my system (using VS2008, .NET 3.5) it worked as expected. Can you set the Visible-property?
confirmDeletesToolStripMenuItem.Visible = false;
-
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 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)); }
Thanks for any clues!
I don't think this is the problem, but just in case: If you check the value on step by step debug the value will always be false, since the window is hidden by visual studio.
-
Hmm? There must be something wrong, but what? I checked your code on my system (using VS2008, .NET 3.5) it worked as expected. Can you set the Visible-property?
confirmDeletesToolStripMenuItem.Visible = false;
johannesnestler wrote:
Can you set the Visible-property?
Yes, I can set the Visible property to either True or False directly. And that works. It is only when trying to toggle it by using the ! operator it always returns True. (Because getting the value of .Visible always returns False) So if I set it either in the property grid or in code, to False. The MenuItem is Not visible as expected. The first toggle using the ! operator then makes it visible, further toggles continue to leave it visible as it just keeps setting it to True. (because this.confirmDeletesToolStripMenuItem.Visible continues to return false!) Without worry about my previous code, to simplify my example, even if I do something like....
this.confirmDeletesToolStripMenuItem.Visible = true;
mbox("Visible = " + this.confirmDeletesToolStripMenuItem.Visible.ToString());I still get False! ??
-
Hmm? There must be something wrong, but what? I checked your code on my system (using VS2008, .NET 3.5) it worked as expected. Can you set the Visible-property?
confirmDeletesToolStripMenuItem.Visible = false;
I had a thought after my last post. 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? Getting closer but stranger?
modified on Saturday, August 28, 2010 3:17 AM