简体   繁体   中英

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

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.

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);
        }
    }

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;

Context Menu in a TreeView. I'm using Visual Studio 2022 and had trouble figuring out this same thing. With a bunch of Google, mostly SO, and experimenting this is what I ended up doing.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM