Search code examples
c#winformstreeviewclick

Winform:TreeView Node Collapse without Node Click event firing


I need to prevent a click event occurring on a node when its collapsed. I still want the node to collapse and hide all the children under it but I don't want the click event being fired or the node selected if possible.


Solution

  • If you only need to influence you own code you can use a flag like this:

    bool suppressClick = false;
    
    private void treeView1_Click(object sender, EventArgs e)
    {
        if (suppressClick) return;
        // else your regular code..
    }
    
    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Node.IsExpanded)
             { suppressClick = false; }
        else { suppressClick = true; }
    }
    

    For more control you may need to get at the windows message queue..