Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

menustrip Problem.

Status
Not open for further replies.

ICTECH

Technical User
Jun 5, 2002
131
CA
Hi All..

I'm trying to setup a menustrip that will show different options acording to the logged in user security clearence. I'm using vb2005 and this is a winform app.

I've build the menustrip with all its options.

Code:
 Me.MenuStrip1.Items.Item("User Manager").Visible = False

But when I try to hide the different menu items (visible = False), I get an error saying "Object reference not set to an instance of an object"

If I use:

Code:
UserManagerToolStripMenuItem

I don't get any error message and the menu item behavior doesn't change. The item stays visible. I have even tried setting the visible to false in the menustrip designer and then at run time set the behavior to true with no change in the menu.

This is all done within the same winform.

Thanks for the help..
 
Hi ICTECH

When you use the menustrip you can call the items by design name.
So when you name the Item "User Manager" for example miUserManager you can simple put

Code:
miUserManager.Visible = True

Or ofcourse

Code:
miUserManager.Visible = False.

Greetings
So the name
 
First, anytime you access a control the name of the control is required and there can be no spaces in a name. So there is not control named "User Manager" so it cannot set it's visibility.

Second, the best way to access a control is if it is created at design is to access the control directly. For instance if the control is called UserManager then it would be UserManager.Visible = False or Me.UserManager.Visible = False. It is generally better to access the control directly during design time if you can to eliminate the chance of further mistakes.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi ErnstJan & Sorwen

I have tried both of your sudgestions and still have no change in the form..
Code:
Me.UserManagerToolStripMenuItem.Visible = False

Could the form need to be redrawn on the screen before the change is seen?
 
ICTECH,

Would suggest to step through your program to see if your code is run or not because it should be working with your code.
No need to redraw the screen.

Greetings,
Ernst Jan
 
Hi ErnstJa..

I agree, but for some reason its not responding.. I have stepped through the code and its being hit with no error messages.. I also have the form as a MDI Container.. That shouldn't cause the menustrip to not respond?
 
Nope, should work fine. So when you step through the lines of code and it hits the line "Me.UserManagerToolStripMenuItem.Visible = False" nothing happens? If you have other menu items try to set each of their visibility one after the other just to test. Actually scratch that, please post your code showing how you are using it because something else just isn't right.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I had similar problem with menustrip, so I decided to use this:
Code:
Dim CertainItemName As String = "Menu1ToolStripMenuItem"
For Each i As ToolStripMenuItem In MenuStrip1.Items
  If (i.Name = CertainItemName) Then
     i.Visible = (Not i.Visible) 'test only
     Exit For
  End If
Next

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top