Anel Hodzic
Anel Hodzic

Reputation: 209

MenuStrip items Enable/Disable vb.net

How to programaticaly Enable or Disable MenuStrip items.

Example if i have this

enter image description here

I want to disable the item2 and item3. Tried with

  MenuStrip1.Items("Item 1").Enabled = False
        MenuStrip1.Items(2).Enabled = False

Upvotes: 3

Views: 11649

Answers (3)

Mirko Tipka
Mirko Tipka

Reputation: 11

I'm using:

ContextMenuStrip1.Items.Item(1).Enabled = False
ContextMenuStrip1.Items.Item(2).Enabled = False

Upvotes: 0

Tony
Tony

Reputation: 697

Just to add to this. I'm using VS Express 2013, and ViewMenuItem.DropDownItems(2).Enabled = False wouldn't work for me.

I found that this did.

ShowRawDataToolStripMenuItem.Enabled = True

My menu name in this instance is "Show raw data" or "ShowRawData"

I hope this helps somebody else.

Upvotes: 0

Going by the image, it appears you want to disable/enable things in the dropdown.

Each top level menu item is itself an object which contains the actual drop down items - the MenuStrip is just a container for them. So, if I have a File | View | Tools menu, there will be three ToolStripMenuItems to work with, each with a DropDownItems collection of those entries. So:

ViewMenuItem.DropDownItems(2).Enabled = False

This disables the 3rd dropdown item on the View menu. Yours might be named ItemsToolStripMenuItem. The UI designer doesn't use a key to create/add new dropdown items, so the string overload wont work unless you are adding them manually:

' create new DD item
Dim foo = New ToolStripMenuItem("Foo", Nothing, 
              AddressOf FooToolStripMenuItem_Click, "Foo")
' add to menu
ViewMenuItem.DropDownItems.Add(foo)

' access by key
ViewMenuItem.DropDownItems("Foo").Enabled = True

Upvotes: 2

Related Questions