Solved

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

Posted on 2006-06-24
3
268 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
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 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Entity Framework 7 43
Service Controller 1 23
Call windows 10 virtual keyboard from windows forms app 2 36
How come this regular expression allows "e"? 14 38
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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