[RESOLVED] Adding context menu items
I have a context menu (mnuContext) created at design-time and I want to modify one entry at runtime. I can't see an obvious way to modify an entry so deleting and adding will be fine. I must be doing something daft as I can't get this to work:
Code:
Dim ContextImageProperites As New ToolStripMenuItem
ContextImageProperites.Text = "Image Properties..."
ContextImageProperites.Tag = "10"
AddHandler ContextImageProperties.Click, AddressOf ContextImageProperties_Click
mnuContext.Items.Add(ContextImageProperites)
...
Private Sub ContextImageProperties_Click(sender As System.Object, e As System.EventArgs)
MsgBox("Clicked Image Properties!")
End Sub
The error is 'ContextImageProperties' is not declared' on the AddHandler line, which seems odd as it's defined as a ToolStripMenuItem. What am I doing wrong?
Re: Adding context menu items
You don't need to delete and add. There is an obvious way to modify an entry. If you added an item to the menu in the designer then there's a member variable declared for that item so you access it via that. For instance, if I add a ContextMenuStrip to a form and then type an item into the designer with the text "First" then a member variable will be added named FirstToolStripMenuItem and you can access the item via it, setting the Text, Tag or whatever.
Even without that field, you could still access the item by index via the Items collection of the ContextMenuStrip. It's a collection like any other. If you can Add items then you can access items by index.
Re: Adding context menu items
it looks like a typo:
ContextImageProperites
should be:
ContextImageProperties
Re: Adding context menu items
Thanks for that. Modifying the existing entry will be best. How do I access an item by index, as mnuContext.Items.Item(3) = "TEST" gives the error "property is read only" ?
Re: Adding context menu items
Quote:
Originally Posted by
.paul.
it looks like a typo:
ContextImageProperites
should be:
ContextImageProperties
:o:o:o:o:o:o:o:o:o:o:o:o:o:o:o:o:o ... thanks Paul ... (hangs head in shame ...)
Re: Adding context menu items
Quote:
Originally Posted by
paulg4ije
Thanks for that. Modifying the existing entry will be best. How do I access an item by index, as mnuContext.Items.Item(3) = "TEST" gives the error "property is read only" ?
Think about it. Is each item a String? Of course not. It's a ToolStripMenuItem, so how can you set an item to a String? Did you read my post? You get the ToolStripMenuItem object and then you set its Text property, Tag property or whatever other property is appropriate.
Re: Adding context menu items
Quote:
Originally Posted by
jmcilhinney
Think about it. Is each item a String? Of course not. It's a ToolStripMenuItem, so how can you set an item to a String? Did you read my post? You get the ToolStripMenuItem object and then you set its Text property, Tag property or whatever other property is appropriate.
I did read your post "you could still access the item by index via the Items collection of the ContextMenuStrip" I just can't seem to get a handle on accessing the collection. The string thing of mine was a bit silly but "Item" is still read only, so there must be another way of accessing the "items" in the collection. Any hints?
Re: Adding context menu items
Quote:
Originally Posted by
paulg4ije
:o:o:o:o:o:o:o:o:o:o:o:o:o:o:o:o:o ... thanks Paul ... (hangs head in shame ...)
i was just looking at your code.
jmcilhinney gave you the right + more appropriate answer.
Re: Adding context menu items
mnuContext.Items.Item(3).Text = "TEST"
Re: Adding context menu items
OK, I think I'm there with something like:
Code:
Dim t As ToolStripItem = mnuContext.Items.Item(3)
Not sure why it has to be ToolStripItem and not ToolStripMenuItem though ...
Thanks.
Re: Adding context menu items
Quote:
Originally Posted by
.paul.
mnuContext.Items.Item(3).Text = "TEST"
Thanks Paul. :wave:
Re: Adding context menu items
Quote:
Originally Posted by
paulg4ije
OK, I think I'm there with something like:
Code:
Dim t As ToolStripItem = mnuContext.Items.Item(3)
Not sure why it has to be ToolStripItem and not ToolStripMenuItem though ...
Thanks.
Because the menu can display any type of item, so the collection has to be able to store any type of item. As such, the Items collection returns a ToolStripItem reference, which is the base class for all items that can appear on a ToolStrip. That's just the type of the reference though. The object will be something else. It might be a ToolStripTextBox, a ToolStripComboBox, a ToolStripMenuItem or whatever. It's up to you to cast the reference as the appropriate type so that you can use it as that type. It's exactly the same as indexing the Controls collection of a form and getting a Control reference, then casting it as type Button or TextBox or ListView or whatever.
Re: Adding context menu items
Thanks jmc. That all makes sense.