We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to configure the expand or collapse behavior in same level of WinForms TreeNavigator?

Platform: WinForms |
Control: TreeNavigator

Configure the expand or collapse behavior

In WinForms TreeNavigator, there is no direct option to expand and collapse TreeMenuItem in the same level and it can be achieved by following below steps.

1. Need to create a custom TreeMenuItem class inherited from TreeMenuItem.

2. Implement properties to hold parent – child relation and collection. 

3. Then implement the MouseDown event to expand or collapes the child TreeMenuItem collection, when clicking on Parent TreeMenuItem.

C#

public class TreeMenuItemAdv : TreeMenuItem
{
    public TreeMenuItemAdv()
    {  }
    private List<TreeMenuItemAdv> m_ChildCollection;
    public List<TreeMenuItemAdv> ChildCollection
    {
        get
        {
            if(m_ChildCollection == null)
            {
               m_ChildCollection = new List<TreeMenuItemAdv>();
            }
            return m_ChildCollection;
        }
    }
 
    /// <summary>
    /// Invoked when the mouse down
    /// </summary>
    protected override void OnMouseDown(MouseEventArgs e)
    {
       if(!this.Collapsed && this.ParentControl != null)
       {
          for (int i = 0; i < this.ChildCollection.Count; i++)
          {
              TreeMenuItemAdv item = this.ChildCollection[i] as TreeMenuItemAdv;
              if(item != null)
              {
                 item.Visible = false;
                 if(this.ParentControl != null)
                    this.ParentControl.SerializeScrollPosition();
                    this.Collapsed = true;
                    item.Invalidate();
              }
          }
       }
       else if(this.Collapsed && this.ParentControl != null)
       {
          for(int i = 0; i < this.ChildCollection.Count; i++)
          {
              TreeMenuItemAdv item = this.ChildCollection[i] as TreeMenuItemAdv;
              if(item != null)
              {
                 item.Visible = true;
                 if(this.ParentControl != null)
                    this.ParentControl.SerializeScrollPosition();
                    this.Collapsed = false;
                    item.Invalidate();
              }
          }
       }
       if(this.ParentControl != null)
       {
          this.ParentControl.UpdateVisibleItemBounds();
          this.ParentControl.ApplySavedScrollPosition();
       }
       base.OnMouseDown(e);
    }
}   

Screenshot:

Show the expand or collapse button in TreeNavigator

Samples:

C#: TreeNavigator_ExpandCollapse_C#

VB: TreeNavigator_ExpandCollapse_VB

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile