• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Displaying a Form When a Node in a TreeView is Right-Clicked

Can someone please provide me with an example of how to do this? I couldn't find any. There is no right mouse click event for the TreeView, for one thing.

All I want to do is pop up a form when a node in a tree view is right clicked.

Thanks!
0
marmaxx
Asked:
marmaxx
  • 2
1 Solution
 
AlexFMCommented:
Use TreeView MouseDown event:

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            if ( e.Button == MouseButtons.Right )
            {
                // If you want to know what node is clicked, use the follwing code:

                Point p = treeView1.PointToClient(Control.MousePosition);
                TreeNode node = treeView1.HitTest(p).Node;   // can be null

                // Show form here, for example:
                Form1 form = new Form1();
                form.ShowDialog(this);
            }
        }
0
 
AlexFMCommented:
Actually, this is simple way:

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            if ( e.Button == MouseButtons.Right )
            {
                TreeNode node = treeView1.HitTest(new Point(e.X, e.Y)).Node;   // can be null

                // Show form here, for example:
                Form1 form = new Form1();
                form.ShowDialog(this);
            }
        }
0
 
vo1dCommented:
you dont need a new point object for the hittest, just use e.Location.
and you should select the clicked node, if you show a specific form for each clicked node.

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Right)
            {
                TreeNode node = this.treeView1.HitTest(e.Location).Node;
                if(node != null)
                {
                    this.treeView1.SelectedNode = node;
                    //show form etc
                }
            }
        }
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now