views:

590

answers:

2

Hi,

I am working TreeView and TreeView.Nodes in my C# GUI application and want to use the right click functionality on a few nodes in my tree. I have searched quite a bit but it seems like the SelectedNode is valid only for left click and there is nothing to capture the right click on a node. I want to add functionalities like 'Add', 'Remove', 'Rename', etc. to the nodes when right clicked upon. Any guidance please?

Thanks, Viren

A: 

Use the ContextMenuStrip property on the TreeView to add a context menu. If you need to not show the menu for some of the nodes, you can handle the ContextMenuStrip's Opening event to cancel it from showing itself -- or, you can disable some of the menu's options from there as well.

Edit: to grab the node under the mouse, handle the MouseUp event on the TreeView, and use this code:

TreeNode nodeUnderMouse = tvMyTreeView.GetNodeAt(e.X, e.Y);
Jon Seigel
+1  A: 

Add a handler for MouseUp. In the handler, check the args for a right mouse button, return if it's not. Call treeView.GetNodeAt() with the mouse coordinates to find the node. Create a context menu.

Here's something similar for a list control which can be adapted for a TreeView:

        private void listJobs_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int index = listJobs.IndexFromPoint(e.Location);
                if (index != ListBox.NoMatches)
                {
                    listJobs.SelectedIndex = index;

                    Job job = (Job)listJobs.Items[index];

                    ContextMenu cm = new ContextMenu();


                    AddMenuItem(cm, "Run", QueueForRun, job).Enabled = !job.Pending;
                    AddMenuItem(cm, "Cancel run", CancelQueueForRun, job).Enabled = (job.State == JobState.Pending || job.State == JobState.Running);
                    AddMenuItem(cm, "Open folder", OpenFolder, job);

                    cm.Show(listJobs, e.Location);
                }
            }
        }

        private MenuItem AddMenuItem(ContextMenu cm, string text, EventHandler handler,     object context)
        {
            MenuItem item = new MenuItem(text, handler);
            item.Tag = context;
            cm.MenuItems.Add(item);
            return item;
        }

You may need to use PointToClient or PointToScreen on the form to translate the coordinates appropriately. You'll soon realize if you need them when the context menu appears in the wrong place.

Scott Langham
I tried using your code and it does not work. I change your code to make my simpler initially as follows:void treeView1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y); Console.WriteLine("Right Clicked"); } }Even this simple piece of code doesnt seem to work. Can you please point out the exact problem. When I right click on a node in the tree it doesnt even select that node.
VP
Hey thanks Scott. It works fine now. I forgot to register my self written event handler with the my GUI forms application. My bad.
VP
Nice work. I'm happy that you solved it.
Scott Langham