Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ToolStripMenuItem.Visible always returns false? (Clarified hopefully)

ToolStripMenuItem.Visible always returns false? (Clarified hopefully)

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AussieLew
    wrote on last edited by
    #1

    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....

    M D 2 Replies Last reply
    0
    • A AussieLew

      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....

      M Offline
      M Offline
      Matt U
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • M Matt U

        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.

        A Offline
        A Offline
        AussieLew
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A AussieLew

          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....

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          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 the Enabled property instead. You could iterate over all the items and set Visible based on Enabled.

              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)

          A 1 Reply Last reply
          0
          • D DaveyM69

            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 the Enabled property instead. You could iterate over all the items and set Visible based on Enabled.

                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)

            A Offline
            A Offline
            AussieLew
            wrote on last edited by
            #5

            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

            D 1 Reply Last reply
            0
            • A AussieLew

              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

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              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)

              A 1 Reply Last reply
              0
              • D DaveyM69

                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)

                A Offline
                A Offline
                AussieLew
                wrote on last edited by
                #7

                Thanks again Dave, The documentation for Available actually explains Visible better than the Visible Docs. I thought that it must have been working correctly, (in an odd way). Just couldn't see it in the documentation. Thanks Lew

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups