Reputation: 1542
I've got a treeview with several treenodes. When I right-click a treenode i get a contextmenu with different options, for example 'delete item'.
Is there a simple way to get the treenode object, that was right-clicked, in the eventHandler of the contextmenu-item ?
Upvotes: 3
Views: 3433
Reputation: 495
Another idea is to set the context menu's Tag property to the node object and then just access it from the event handler. Of course, this only works if you aren't using Tag for anything else.
private void MyTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
MyContextMenu.Tag = e.Node;
MyContextMenu.Show(this, e.Location);
}
}
private void MyToolStripMenuItem_Click(object sender, EventArgs e)
{
//Get TreeNode from Tag
//Note: Could also get ContextMenu from sender,
//but we already have it, so just access it directly
TreeNode node = MyContextMenu.Tag as TreeNode;
if (node == null)
return;
//Do stuff with node here
}
Upvotes: 0
Reputation: 906
If you (right)click a node, doesn't it become the selected node?
TreeNode needed = TreeViewX.SelectedNode;
Cheers
Upvotes: 0
Reputation: 2372
I had similar problem some time ago and I came up with solution like this
Create your own myContextMenuStrip class which derives from standard ContextMenuStrip
public class myContextMenuStrip : ContextMenuStrip
{
public TreeNode tn;
public myContextMenuStrip() { }
protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
{
base.OnItemClicked(e);
if (e.ClickedItem.Text == "asd") MessageBox.Show(tn.Text);
}
}
Inside you're overriding OnItemClicked method, which will be displaying MessageBox when you click particular menuItem.
So, when you click on treeView item with right mouse button, it will retrieve node from under your mouse pointer and pass it to your myContextMenuStrip.
private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
TreeNode tn = treeView1.GetNodeAt(e.Location);
myMenu.tn = tn;
}
}
And on formLoad you're initializing your myContextMenuStrip, adding items, and binding it to treeView.
private void Form1_Load(object sender, EventArgs e)
{
myMenu = new myContextMenuStrip();
myMenu.Items.Add("asd");
treeView1.ContextMenuStrip = myMenu;
}
I know it's not very elegant way, but it's simple and it works (contrary to the idea of passing treeNode value inside EventArgs which could be difficult to implement or even impossible - didn't try myself, had few attempts but resigned).
Whole working code, treeView on form needed:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public myContextMenuStrip myMenu;
private void treeView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
TreeNode tn = treeView1.GetNodeAt(e.Location);
myMenu.tn = tn;
}
}
private void Form1_Load(object sender, EventArgs e)
{
myMenu = new myContextMenuStrip();
myMenu.Items.Add("asd");
treeView1.ContextMenuStrip = myMenu;
}
public class myContextMenuStrip : ContextMenuStrip
{
public TreeNode tn;
public myContextMenuStrip() { }
protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
{
base.OnItemClicked(e);
if (e.ClickedItem.Text == "asd") MessageBox.Show(tn.Text);
}
}
}
}
Upvotes: 6