?
Solved

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

Posted on 2006-06-24
3
Medium Priority
?
295 Views
Last Modified: 2010-04-16
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
Comment
Question by:marmaxx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16977646
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 16977649
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
 
LVL 11

Accepted Solution

by:
vo1d earned 2000 total points
ID: 16980771
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question