Right click on TreeView: Get the node clicked at

Problem:

There is a TreeView control with n number of nodes. When the user right clicks on the TreeView control, get the node on which the right click was performed.

Background:

In TreeView control, SelectedNode property is set whenever left click occurs. This is not true for right clicks. So, we need to have some workaround for this.

Solution 1:

The most popular solution is to handle the MouseDown event. Check if the click was a right click and then use the TreeView control’s GetNodeAt API to get the node which was clicked at.

private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
TreeNode selectedNode = treeView1.GetNodeAt(e.X, e.Y);
MessageBox.Show("You clicked on node: " + selectedNode.Text);
}
}


Solution 2:

Handle the NodeMouseClick event. The TreeNodeMouseClickEventArgs has the node which was clicked at.

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MessageBox.Show("You clicked on node: " + e.Node.Text, "Solution 2");
}
}

SelectedNode property of a treeview is set whenever a left click occurs. Why can’t the same be applied for right click? Right click on a treeview is such a common UI event, that users will benefit from it.

22 thoughts on “Right click on TreeView: Get the node clicked at

  1. fneep says:

    Excellent explanation and solutions. I wish Microsoft would just make a NodeMouseRightClick() event but until they do, this is the next best thing.

  2. “SelectedNode property of a treeview is set whenever a left click occurs. Why can’t the same be applied for right click?”

    This would cause an inconsistency for instance when you display a context menu and the user cancels the menu.

  3. Emil Essa says:

    I need to thank you very much and need to tell you that I’m use this code to select not in right and left click

    Me.TreeView1.SelectedNode = e.Node

  4. vytheese says:

    Excellent !! Thank you.

  5. Aravind says:

    we simply need to handle the NodeMouseClickEvent.

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)

    {

    if (e.Button == MouseButtons.Right)

    {

    this.treeView1.SelectedNode = e.Node;

    }

    }

  6. Alexwebmaster says:

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

  7. sandrar says:

    Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.

  8. Real Drouin says:

    The exemple is fine for a WPF application but it doesn’nt work for a .Net application. That TreeNodeMouseClickEventArgs e) doesn’t exist.

  9. CrashPilot says:

    Excelent and usefull post, made my life a lot easier with the use of context windows, exactly what I was looking for

  10. Soul says:

    I want open a menu (example : “Open”;”Exit”) after right click on treeview .
    How to do it?
    Thanks!

  11. Duncan says:

    Cudos to Emil Essa…

    Just use E….e’s are good…as the old song says..

  12. Thanks for these simple solutions to the right-click issue I was having with a treeview.

  13. PennyForem says:

    A lovely sunny day here on the Wirral peninsula.

  14. Zgdtllmz says:

    Excellent work, Nice Design http://otyfirehuruj.de.tl japanese younger model i’ve been a fan of shyla since the first time i saw her, but i don’t think i’ve ever seen her look so good. she looks absolutely amazing in this video.

  15. Ecjajrtp says:

    A pension scheme http://nusoetogimyc.de.tl little lolta sex she gets a nice fat cock and loving it, look how he can slide in, would love to try that cock

  16. Neetesh says:

    IS is code of WPf or Windows Application????????????????

  17. I am really enjoying the theme/design of your site. Do you ever run into any internet browser compatibility issues? A few of my blog visitors have complained about my website not working correctly in Explorer but looks great in Chrome. Do you have any recommendations to help fix this issue?

  18. Ovenrelt says:

    http://www.pimpblog.nl/dapoxetine30mg96 Effects of long-term remedy with Serenoa repens (Permixon) about the concentrations of mit as well as localized distribution involving androgens along with skin development aspect in civilized prostatic hyperplasia

  19. Zexhoime says:

    Generic Priligy These types of men wanted to get Priligy as well as acquire dapoxetine as it is a protracted anticipated remedy for a condition known as Rapid ejaculation or Uncontrolled climaxes, that’s been the origin involving stress and panic for men forever of your energy

Leave a comment