Search code examples
c#treeview

C# TreeView Collapse/Expand on parent select


This one is hard to explain so i will just tell you what i want to achieve and how i am doing it so maybe someone witll have an idea on what i am doing wrong.

In my winForm i have a TreeView control that i populate. All parents have children (any number of them) but no children have children of their own. Example:

- Parent 1
        Child 1
        Child 2
        Child 3
- Parent 2
       Child 4

The behaviour i want is the following:

  1. when the user clicks a child it get's selected (done by default)
  2. When the user clicks a parent, it should either collapse or expand but not change any selection

Point 1 is no problem as it is the default behaviour of a TreeView, Point 2 however is another story.

Here is what i have done so far:

To prevent the parent from being selected i add a BeforeSelect event handler and put in it the following code:

if (e.Node.Parent == null)
    e.Cancel = true;

This does the job perfectly. So now that i canceled the selection of the parent i want to expand or collapse it. So i changed the above code to:

if (e.Node.Parent == null)
{
    if (e.Node.IsExpanded)
    {
        e.Node.Collapse();
    }
    else
    {
        e.Node.Expand();
    }
    e.Cancel = true;
}

This somewhat works - except that, unlike clicking the + sign, calling Expand() or Collapse() makes the parent node be selected, ignoring the e.Cancel = true; line.

I tried getting the SelectedNode before i call Collapse() or Expand() and then setting it back - it works but when i do that it will expand the parent again to make the selection.

Somewhow the + sign does exactly what i want to happen when i click on a parent - i just can't seem to get it to work.

Ideas?

Thanks in advance


Solution

  • try this:

        private bool allowExpandCollapse = false;
    
        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            e.Cancel = !allowExpandCollapse;
        }
    
        private void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
        {
            e.Cancel = !allowExpandCollapse;
        }
    
        private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Parent == null)
            {
                e.Cancel = true;
            }
        }
    
        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Parent == null)
            {
                allowExpandCollapse = true;
                if (e.Node.IsExpanded)
                {
                    TreeNode selectedNode = treeView1.SelectedNode;
                    e.Node.Collapse();
                    allowExpandCollapse = false;
                    treeView1.SelectedNode = selectedNode;
                }
                else
                {
                    e.Node.Expand();
                    allowExpandCollapse = false;
                }
            }
        }