Solved

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

Posted on 2006-06-24
3
247 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
  • 2
3 Comments
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now