开发者

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

开发者 https://www.devze.com 2023-04-01 17:41 出处:网络
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 t

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

精彩评论

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

关注公众号