如何将鼠标点击事件添加到winform treenode?

时间:2011-09-01 21:02:58

标签: treenode

如何将鼠标点击事件添加到winform treenode?

更新

请注意,我想在运行时执行此操作。

1 个答案:

答案 0 :(得分:2)

要动态执行此操作,您需要处理TreeView的NodeMouseClick事件:

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);
        }
    }
}