C # Right click on TreeView nodes

I have a TreeView with parent node: Node0 . I add 3 subnodes :

 Node01 Node02 Node03 

I have a popup menu that is associated with each of the sub-pods.

My problem: if I right-clicked directly on one of the sub-bases, my pop-up window will not appear. So I need to first select Subnode and right-click to display a popup.

  • How can I change the code so that Direct Right-Click on a specific SubNode opens PopupMenu?
  • In popupMenu, there is only an OpenMe menu in the list. When you click on this menu, the windows should open, and these windows should be associated with the submenu that I clicked. How to get an event in the right submenu with the right mouse button and display a form with it?

EDIT:

Look at this

 private void modifySettingsToolStripMenuItem_Click(object sender, EventArgs e) { try { String s = treeView1.SelectedNode.Text; new chartModify(s).ShowDialog(); } catch (Exception er) { System.Console.WriteLine(">>>" + er.Message); } } 

Line String s = treeView1.SelectedNode.Text; gets the name of the selected node, not the node that was right-clicked.

So here I have to change this piece of code with

 private void treeview1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Button == MouseButtons.Right) MessageBox.Show(e.Node.Name); } 

I modify it like this:

 try { TreeNodeMouseClickEventArgs ee; new chartModify(ee.Node.Name).ShowDialog(); } 

but it does not work: Error:Use of unassigned local variable 'ee'

EDIT # 2: Finaly got a solution

 public string s; private void modifySettingsToolStripMenuItem_Click(object sender, EventArgs e) { try { new chartModify(s).ShowDialog(); } catch (Exception er) { System.Console.WriteLine(">>>" + er.Message); } } 

and then

 private void treeview1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Button == MouseButtons.Right) { s = e.Node.Name; menuStrip1.Show(); } } 

it works,
Thanks

+4
source share
2 answers

You can try to use the NodeMouseClick Event, which uses the TreeNodeClickEventArgs to get the button and the Node that was clicked.

 private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if(e.Button == MouseButtons.Right) MessageBox.Show(e.Node.Name); } 

Changed code to display popup and generated form

 public partial class Form1 : Form { string clickedNode; MenuItem myMenuItem = new MenuItem("Show Me"); ContextMenu mnu = new ContextMenu(); public Form1() { InitializeComponent(); mnu.MenuItems.Add(myMenuItem); myMenuItem.Click += new EventHandler(myMenuItem_Click); } void myMenuItem_Click(object sender, EventArgs e) { Form frm = new Form(); frm.Text = clickedNode; frm.ShowDialog(this); clickedNode = ""; } private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { if (e.Button == MouseButtons.Right) { clickedNode = e.Node.Name; mnu.Show(treeView1,e.Location); } } } 
+17
source

This will give you treenode at a specific point in the mouse when right-clicking.

 if(e.Button == MouseButtons.Right) { TreeNode destinationNode = ((TreeView)sender).GetNodeAt(new Point(eX, eY)); //Do stuff } 

Here you can open a specific pop-up menu.

+2
source

All Articles