|
-
Jan 20th, 2013, 07:13 AM
#1
Thread Starter
Fanatic Member
[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?
Last edited by paulg4ije; Jan 20th, 2013 at 07:22 AM.
-
Jan 20th, 2013, 07:22 AM
#2
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.
-
Jan 20th, 2013, 07:23 AM
#3
Re: Adding context menu items
it looks like a typo:
ContextImageProperites
should be:
ContextImageProperties
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 20th, 2013, 07:27 AM
#4
Thread Starter
Fanatic Member
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" ?
-
Jan 20th, 2013, 07:29 AM
#5
Thread Starter
Fanatic Member
Re: Adding context menu items
-
Jan 20th, 2013, 07:34 AM
#6
Re: Adding context menu items
 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.
-
Jan 20th, 2013, 07:41 AM
#7
Thread Starter
Fanatic Member
Re: Adding context menu items
 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?
-
Jan 20th, 2013, 07:42 AM
#8
Re: Adding context menu items
 Originally Posted by paulg4ije
i was just looking at your code.
jmcilhinney gave you the right + more appropriate answer.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 20th, 2013, 07:49 AM
#9
Re: Adding context menu items
mnuContext.Items.Item(3).Text = "TEST"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 20th, 2013, 07:49 AM
#10
Thread Starter
Fanatic Member
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.
-
Jan 20th, 2013, 07:57 AM
#11
Thread Starter
Fanatic Member
Re: Adding context menu items
 Originally Posted by .paul.
mnuContext.Items.Item(3).Text = "TEST"
Thanks Paul.
-
Jan 20th, 2013, 08:01 AM
#12
Re: Adding context menu items
 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.
-
Jan 20th, 2013, 09:30 AM
#13
Thread Starter
Fanatic Member
Re: Adding context menu items
Thanks jmc. That all makes sense.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|