• Adding right-click menu (VB.Net)

    Home » Forums » Developers, developers, developers » DevOps Lounge » Adding right-click menu (VB.Net)

    Author
    Topic
    #386105

    I have a tree view control and I want to add a right-click menu. The items in the menu will change based on the item selected. (I will create multiple context menus and decide at run-time which one to display.)

    I see that .Net calls this type of menu a “context menu” and the VS help text has code showing how to create it and to attach an event to its selection. I can not, however find code to assign it to the treeview. brickwall How does my program know to assign a given context menu to a given control? That’s the question cranky

    In VB or VBA I’d just write code to the right-click event but there is no such thing with the treeview (or other controls it seems). disappointed

    Does anyone have code samples that show how to do this? This context menu will have items such as Add, Delete, Copy, etc.

    Thnx, Deb

    Viewing 1 reply thread
    Author
    Replies
    • #668571

      Deb,

      Sorry, not sure about .NET…

      but even in VB there’s no right click event- it’s a MouseDown event, with the key pressed as a parameter.

      • #668723

        Many controls in Excel VBA have a right-click event so that’s what I’m used to. I ended up finding good stuff in a book by Matthew MacDonald called “User Interfaces in VB.Net:”. The way the right-click event works is that after the item is selected (in my case a node in a tree view), an event is fired (no surprise there). At this point one of the arguments in the event code points to a list of events applicable to that control. You query that event structure for the reason why the event was called. One of the reasons available is which key was used to get there. Voila! bingo

        It take so long to figure out something so simple in .Net sometimes (at least it’s hard because I can’t just write the code I know how to write, I gotta learn something new *ha*).

        Thnx, Deb

    • #669403

      Deb,
      Open the Toolbox and drag and drop “ContextMenu” to your form.
      Bingo. You’ll see a new pane appear (I forget what they call it) with
      a new instance of the contextmenu called contextMenu1.

      Now open the Properties pane for the form. You’ve now got a ContextMenu
      property. Have at it…

    Viewing 1 reply thread
    Reply To: Adding right-click menu (VB.Net)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: