开发者

How do I add a mouseclick event to a winform treenode?

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜