ToolStripMenuItem Possible bug in VS2005


G

Guest

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

Advertisements

T

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

Advertisements

S

steeger2002

genojoe said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top