Reputation: 4655
How to disable all menu items in context menu which contain separator item?
My approach:
For Each item As ToolStripMenuItem In ContextMenuStrip1.Items
item.Enabled = False
Next
work good if I haven't separator in menu but with separator I get error:
Unable to cast object of type 'System.Windows.Forms.ToolStripSeparator' to type 'System.Windows.Forms.ToolStripMenuItem'.
How to disable all items in menu which contain separator item?
Upvotes: 2
Views: 1959
Reputation: 28530
Code example (untested) to illustrate my comment:
For i = 0 To ContextMenuStrip1.Items.Count - 1
If TypeOf ContextMenuStrip1.Items(i) Is ToolStripMenuItem Then
CType(ContextMenuStrip1.Items(i), ToolStripMenuItem).Enabled = False
End If
Next
Basically you go through all the items in the menu, and if the current item is of type ToolStripMenuItem
you disable it.
Upvotes: 2