locked
ContextMenuStrip RRS feed

  • Question

  • Hi i have a little problem.
    i created a context menu using contextmenustrip in vb.net, whit 2 choise "new" and delete .. how i can catch the user choose? (the event)
    if anyone can give me a little example would be really grat .. ty


    Monday, July 7, 2008 2:47 PM

Answers

  • Each item you add to the contextmenustrip is actually its own object. The context menu strip is simply a collection of these objects.

    The actual object type is ToolStripMenuItem

    So if you made one of them have the text "new" then by default Visual Studio will name that ToolStripMenuItem NewToolStripMenuItem and delete would be called DeleteToolStripMenuItem. You can rename these if you wish, or keep the default names.

    Since each is it's own object, each also fires their own events. If you view your contextmenustrip in the form designer, and double click the item you want to write code for, you will be brought to the Click event code for just that one item, either the new or delete, etc..

    The stub code should look like this, and you just need to fill in the middle with your own code.

        Private Sub NewToolStripMenuItem_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click  
            'your code here for New item click  
        End Sub 
     
        Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click  
            'your code here for delete item click  
        End Sub 

    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com - visit my website for a chance to win an MSDN subscription.
    Monday, July 7, 2008 4:15 PM
    Moderator

All replies

  • Each item you add to the contextmenustrip is actually its own object. The context menu strip is simply a collection of these objects.

    The actual object type is ToolStripMenuItem

    So if you made one of them have the text "new" then by default Visual Studio will name that ToolStripMenuItem NewToolStripMenuItem and delete would be called DeleteToolStripMenuItem. You can rename these if you wish, or keep the default names.

    Since each is it's own object, each also fires their own events. If you view your contextmenustrip in the form designer, and double click the item you want to write code for, you will be brought to the Click event code for just that one item, either the new or delete, etc..

    The stub code should look like this, and you just need to fill in the middle with your own code.

        Private Sub NewToolStripMenuItem_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click  
            'your code here for New item click  
        End Sub 
     
        Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click  
            'your code here for delete item click  
        End Sub 

    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com - visit my website for a chance to win an MSDN subscription.
    Monday, July 7, 2008 4:15 PM
    Moderator
  • thanks a lot :) i find out that is like a new obj couse i was looking for the wrong stuff.
    Cya and have a good day.

    Thursday, July 10, 2008 9:49 AM