'Right click on Node in TreeView and have a menu pop up with the option of "Open with" using c# [closed]

I'm working in a Windows form and I have a TreeView with a bunch of nodes. I want to be able to right click on a node and have a menu pop up with an "Open with" option. Much like when you right click on a file and say open with windows media player.

I know how to make the mouse click event handler, I'm just not sure what to do next.

Any advice would be greatly appreciated.



Solution 1:[1]

Here is a simple way to do with a right click

private void Treeview1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right) 
        {
            ContextMenu.Show(Cursor.Position);
        }
    }

Solution 2:[2]

This was a clever answer by Capn Jack in his question Setting the ContextMenuStrip for all nodes on a level in a TreeView? on how to make the context menu show for the right-clicked node also when it is not selected first:

treeView.NodeMouseClick += (sender, args) => treeView.SelectedNode = args.Node;

Solution 3:[3]

Context Menu in a TreeView. I'm using Visual Studio 2022 and had trouble figuring out this same thing. I have 3 of them and spent a bunch of time trying to figure out which had called the context. With a bunch of Google, mostly SO, and experimenting this is what I ended up doing.

Add this to the Form Load to select the node with a right click.

    treeView1.MouseDown += (sender, args) => treeView1.SelectedNode = treeView1.GetNodeAt(args.X, args.Y);

Drop a Context Menu Strip onto the Form, add whatever Items you need, and assign it to the TreeView ContextMenuStrip property.

enter image description here

enter image description here

Select the context menu strip and you should see this in Designer:

enter image description here

If you have one TreeView:

Double click on the item to get the event.

    private void treeViewCopy_Click(object sender, EventArgs e)
    {
        Clipboard.SetText(treeView1.SelectedNode.Text);
    }

If you have more than one TreeView:

If they all use the same context, assign the Context Menu Strip to each TreeView.

If the contexts are different build out new Context Menus and assign them appropriately and from there you could build out each item's click. Or you could follow this.

Add this to Form Load:

        treeViewResults.ContextMenuStrip.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(treeViewResults_ContextClick);
        treeViewEntity.ContextMenuStrip.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(treeViewEntity_ContextClick);
        treeView1.ContextMenuStrip.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(treeView1_ContextClick);

And add the functions:

    private void treeViewEntity_ContextClick(object sender, ToolStripItemClickedEventArgs e)
    {
        GetTree(treeViewEntity, e.ClickedItem.Text);  
    }  
     private void treeView1_ContextClick(object sender, ToolStripItemClickedEventArgs e)
    {
        GetTree(treeView1, e.ClickedItem.Text);         
    }        
    private void treeViewResults_ContextClick(object sender, ToolStripItemClickedEventArgs e)
    {
        GetTree(treeViewResults, e.ClickedItem.Text); 
    }  

All of these will run if the if using the same context menu so you could just switch on the command, but if they are different then this:

    private void GetTree(TreeView tree, string command)
    {
        if (tree.Focused && tree.SelectedNode != null)
        {
            switch (tree.Name)
            {
                case "treeViewEntity":
                    break;
                case "treeView1":
                    treeView1Context(treeView1, command);
                    break;
                case "treeViewResults":
                    break;
            }
        }
    } 

And finally this:

    private void treeView1Context(TreeView tree, string command)
    { 
            switch (command)
            {
                case "Copy":
                       Clipboard.SetText(tree.SelectedNode.Text);                            
                    break;
                case "Edit":
                    break;

            }                    
        
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dchaps
Solution 2 Christopher Hamkins
Solution 3