Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I right click to select a node in a treeview control

Why I right click on a node in my treeview the focus moves to this node and then immediately back to the previously selected node. Is there some way that I can allow the right click to select the node?

like image 979
Matt Wilko Avatar asked Aug 31 '11 13:08

Matt Wilko


1 Answers

That's because the highlight color performs two duties, it shows the selected node and shows the focused node. If you don't do anything with the right-click event then it jumps back to the selected node. The workaround is to select the node:

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
        if (e.Button == MouseButtons.Right) treeView1.SelectedNode = e.Node;
    }

Plus anything else you want to do, usually displaying a context menu.

like image 68
Hans Passant Avatar answered Oct 12 '22 14:10

Hans Passant