Results 1 to 13 of 13

Thread: [RESOLVED] Adding context menu items

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Adding context menu items

    it looks like a typo:

    ContextImageProperites

    should be:

    ContextImageProperties

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    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" ?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Adding context menu items

    Quote Originally Posted by .paul. View Post
    it looks like a typo:

    ContextImageProperites

    should be:

    ContextImageProperties
    ... thanks Paul ... (hangs head in shame ...)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding context menu items

    Quote Originally Posted by paulg4ije View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Adding context menu items

    Quote Originally Posted by jmcilhinney View Post
    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?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Adding context menu items

    Quote Originally Posted by paulg4ije View Post
    ... thanks Paul ... (hangs head in shame ...)
    i was just looking at your code.
    jmcilhinney gave you the right + more appropriate answer.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Adding context menu items

    mnuContext.Items.Item(3).Text = "TEST"

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    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.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    Re: Adding context menu items

    Quote Originally Posted by .paul. View Post
    mnuContext.Items.Item(3).Text = "TEST"
    Thanks Paul.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding context menu items

    Quote Originally Posted by paulg4ije View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    774

    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
  •  



Click Here to Expand Forum to Full Width