SoFunction
Updated on 2025-03-01

There are two ways to add right-click fast key menu in TreeView

One is to use the ContextMenuStrip property of TreeView to add a new ContextMenuStrip. This method is very simple and direct. The disadvantage is that the right-click menu is the response of the entire control, which means that even if you don’t right-click to select the node, it will trigger the display of the shortcut menu.

This method is used to obtain which node to select it:

Copy the codeThe code is as follows:

TreeNode curNode = (, )

Another way is to create a ContextMenuStrip and use the TreeView's NodeMouseClick event, which is implemented in the event as:
Copy the codeThe code is as follows:

private void trvFolder_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if ( == )
            {
                Point pos = new Point( + , + / 2);
                (, pos);
            }
}