Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Event Handling TreeView

capitolccapitolc Member Posts: 31
I'm looking to add event handling to the nodes within a TreeView. I have not found any useful examples/tutorials online given this topic. My question is, is it possible to add a click event (or any other event) to the nodes (parents/child) inside the TreeView component?


-=The Best Has Yet To Come=-

Comments

  • weiccoweicco Member Posts: 950
    : I'm looking to add event handling to the nodes within a TreeView. I have not found any useful examples/tutorials online given this topic. My question is, is it possible to add a click event (or any other event) to the nodes (parents/child) inside the TreeView component?
    :
    :
    : -=The Best Has Yet To Come=-
    :

    No you can't add event handlers to individual nodes. But you can add handlers to treeview like this:

    [code]
    treeView1.AfterSelect += delegate(object sender, TreeViewEventArgs e)
    {
    TreeNode senderNode = e.Node;
    };
    [/code]

    Of course you could derive your own treenode class and add selected event to it and call that from treeview's AfterSelect handler.

    [code]
    class MyTreeNode : TreeNode
    {
    public event MyTreeNodeSelected OnSelected;

    public void CallOnSelected()
    {
    if (OnSelected != null)
    {
    OnSelected(this);
    }
    }
    }

    delegate void MyTreeNodeSelected(MyTreeNode sender);

    // Form1_Load handler
    MyTreeNode node = new MyTreeNode();
    node.Text = "FOOBAR";
    node.OnSelected += delegate(MyTreeNode s)
    {
    MessageBox.Show(s.Text);
    };
    treeView1.Nodes.Add(node);

    // treeView1_AfterSelect
    if (e.Node is MyTreeNode)
    {
    MyTreeNode tmp = e.Node as MyTreeNode;
    tmp.CallOnSelected();
    }
    [/code]
Sign In or Register to comment.