Home All Groups Group Topic Archive Search About

ToolStripMenuItem Possible bug in VS2005

Author
11 Oct 2006 6:36 PM
genojoe
I begin a new form.  I add a MenuStrip.  I then  right click it and select
"Insert Standard Items".  This results in a loaded MenuStrip that contains
File>>New among others.

I then add a button and give it the following code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
   Static b As Boolean
   b = Not b
   NewToolStripMenuItem.Visible = b
   Debug.Print(NewToolStripMenuItem.Visible.ToString)
End Sub

This code toggles the visibility of the "New" menu item.  This code works
correctly regarding the item visibility.  Unfortunately, my Debug.Print
statement always prints "False" whether or not it is visible.  Needless to
say, this causes me problems because I have sections of code that determine
what to do based upon a menu item being visible.

Can someone confirm this behaviour and suggest a different way for
determining if a menu item is visible.  My only apparent alternative would be
to create a form level boolean variable that would shadow whether the menu
item is visible but this seems like a dumb way to resolve the issue.

Needless to say, this was not a problem with the old MainMenu control.

Author
11 Oct 2006 6:52 PM
teslar91
I've verified your results.

On top-level menu items which are always visible, reading .Visible
works properly.

On sub-menu items which are not currently displayed, reading .Visible
always returns False, regardless of whether or not they *would* be
visible once that item is displayed.  Which kinda makes sense if you
think about it, but it's not what you want.

So yes, you will have to shadow the property in some way.  You could
toggle .Enabled to the same value and read it instead.
Author
9 Dec 2006 5:15 PM
steeger2002
genojoe wrote:
Show quoteHide quote
> I begin a new form.  I add a MenuStrip.  I then  right click it and select
> "Insert Standard Items".  This results in a loaded MenuStrip that contains
> File>>New among others.
>
> I then add a button and give it the following code.
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>    Static b As Boolean
>    b = Not b
>    NewToolStripMenuItem.Visible = b
>    Debug.Print(NewToolStripMenuItem.Visible.ToString)
> End Sub
>
> This code toggles the visibility of the "New" menu item.  This code works
> correctly regarding the item visibility.  Unfortunately, my Debug.Print
> statement always prints "False" whether or not it is visible.  Needless to
> say, this causes me problems because I have sections of code that determine
> what to do based upon a menu item being visible.
>
> Can someone confirm this behaviour and suggest a different way for
> determining if a menu item is visible.  My only apparent alternative would be
> to create a form level boolean variable that would shadow whether the menu
> item is visible but this seems like a dumb way to resolve the issue.
>
> Needless to say, this was not a problem with the old MainMenu control.

I've encountered the same problem and found the Available property.

This seems to work in all cases.