How do I add a mouseclick event to a winform treenode?
Update
Note that I want to do t开发者_如何学编程his at runtime.
To do this dynamically you need to handle the TreeView's NodeMouseClick event thusly:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
treeView1.NodeMouseClick +=
new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);
treeView1.Nodes.Add(new TreeNode("Node 1"));
treeView1.Nodes.Add(new TreeNode("Node 2"));
}
void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
Console.WriteLine("Clicked: " + e.Node.Text);
}
}
}
精彩评论