SoFunction
Updated on 2025-03-06

Summary of DevExpress's TreeList usage example

This article summarizes the usage of TreeList in DevExpress, hoping to help everyone learn C# programming. Specific examples are as follows:

using System;
using ;
using ;
using ;
using ;
using ;
using ;

namespace DevExpressUtilHelpV3
{
  public static class TreeListToolV3
  {
    public delegate string BuildPathRule(string nodeText, string fullPathInfo);
    /// <summary>
    /// Get all information from the selected node to the root node    /// </summary>
    /// <param name="focusedNode">TreeListNode</param>
    /// <param name="columnID">Column name</param>    /// <param name="buildPathRule">Rules delegation</param>    /// <returns>Path information</returns>    public static string FullPathInfo(this TreeListNode focusedNode, string columnID, BuildPathRule buildPathRule)
    {
      if (focusedNode == null)
        throw new ArgumentNullException("focusedNode");
      if (("columnID"))
        throw new ArgumentNullException("columnID");
      string _fullPathInfo = ;
      _fullPathInfo = (columnID);
      while ( != null)
      {
        focusedNode = ;
        string _nodeText = (columnID).Trim();
        _fullPathInfo = buildPathRule(_nodeText, _fullPathInfo);
      }
      return _fullPathInfo;
    }
    public delegate bool CompareNodeRule(TreeListNode focusedNode);
    /// &lt;summary&gt;
    /// Get all information from filtering nodes to root nodes    /// &lt;/summary&gt;
    /// &lt;param name="focusedNode"&gt;TreeListNode&lt;/param&gt;
    /// <param name="columnID">Column name</param>    /// <param name="compareNodeRule">Rules delegation</param>    /// <param name="buildPathRule">Rules delegation</param>    /// <returns>Path information</returns>    public static string FilterPathInfo(this TreeListNode focusedNode, string columnID, CompareNodeRule compareNodeRule, BuildPathRule buildPathRule)
    {
      if (focusedNode == null)
        throw new ArgumentNullException("focusedNode");
      if (("columnID"))
        throw new ArgumentNullException("columnID");
      string _fullPathInfo = ;
      _fullPathInfo = (columnID);
      while ( != null)
      {
        focusedNode = ;
        if (compareNodeRule(focusedNode))
        {
          string _nodeText = (columnID).Trim();
          _fullPathInfo = buildPathRule(_nodeText, _fullPathInfo);
        }
      }
      return _fullPathInfo;
    }
    /// &lt;summary&gt;
    /// Recursively traverse tree nodes    /// &lt;/summary&gt;
    /// &lt;param name="tree"&gt;&lt;/param&gt;
    /// &lt;param name="opreateRule"&gt;&lt;/param&gt;
    public static void LoopTree(this TreeList tree, Action&lt;TreeListNode&gt; opreateRule)
    {
      if (tree == null)
        throw new ArgumentNullException("tree");
      foreach (TreeListNode node in )
      {
        opreateRule(node);
        if ( &gt; 0)
        {
          LoopTreeNodes(node, opreateRule);
        }
      }
    }
    /// &lt;summary&gt;
    /// Recursively traverse TreeListNode node    /// &lt;/summary&gt;
    /// &lt;param name="node"&gt;&lt;/param&gt;
    /// &lt;param name="opreateRule"&gt;&lt;/param&gt;
    public static void LoopTreeNodes(this TreeListNode node, Action&lt;TreeListNode&gt; opreateRule)
    {
      if (node == null)
        throw new ArgumentNullException("node");
      foreach (TreeListNode _childNode in )
      {
        opreateRule(_childNode);
        LoopTreeNodes(_childNode, opreateRule);
      }
    }
    /// &lt;summary&gt;
    /// Recursively traverse TreeListNode, stop looping when preventRule returns false    /// &lt;/summary&gt;
    /// &lt;param name="node"&gt;TreeListNode&lt;/param&gt;
    /// &lt;param name="opreateRule"&gt;Func&lt;TreeListNode, bool&gt;&lt;/param&gt;
    public static void LoopTreeNodes_Break(this TreeListNode node, Func&lt;TreeListNode, bool&gt; opreateRule)
    {
      if (node == null)
        throw new ArgumentNullException("node");
      foreach (TreeListNode _childNode in )
      {
        if (!opreateRule(_childNode))
          break;
        LoopTreeNodes_Break(_childNode, opreateRule);
      }
    }
    /// &lt;summary&gt;
    /// Recursively traverse TreeListNode, when preventRule returns false, jump out of the loop, directly enter the next loop    /// &lt;/summary&gt;
    /// &lt;param name="node"&gt;TreeListNode&lt;/param&gt;
    /// &lt;param name="opreateRule"&gt;Func&lt;TreeListNode, bool&gt;&lt;/param&gt;
    public static void LoopTreeNodes_Continue(this TreeListNode node, Func&lt;TreeListNode, bool&gt; opreateRule)
    {
      if (node == null)
        throw new ArgumentNullException("node");
      foreach (TreeListNode _childNode in )
      {
        if (!opreateRule(_childNode))
          continue;
        LoopTreeNodes_Continue(_childNode, opreateRule);
      }
    }
    public delegate bool CheckNodeRule(TreeListNode fucusedNode);
    public delegate void CheckNodeNullRule();
    /// &lt;summary&gt;
    /// Check the node for null    /// &lt;/summary&gt;
    /// &lt;param name="fucusedNode"&gt;TreeListNode&lt;/param&gt;
    /// <param name="checkNodeRule">If it is NULL, processing logic</param>    /// &lt;returns&gt;TreeListNode&lt;/returns&gt;
    public static TreeListNode CheckNull(this TreeListNode fucusedNode, CheckNodeNullRule checkNodeRule)
    {
      if (fucusedNode == null)
      {
        checkNodeRule();
        return null;
      }
      return fucusedNode;
    }
    /// &lt;summary&gt;
    /// Check logic for node    /// &lt;/summary&gt;
    /// &lt;param name="fucusedNode"&gt;TreeListNode&lt;/param&gt;
    /// <param name="checkNodeRule">Check logic code [delegation]</param>    /// &lt;returns&gt;TreeListNode&lt;/returns&gt;
    public static TreeListNode Check(this TreeListNode fucusedNode, CheckNodeRule checkNodeRule)
    {
      if (fucusedNode != null)
        return checkNodeRule(fucusedNode) == true ? fucusedNode : null;
      return null;
    }
    /// &lt;summary&gt;
    /// Horizontal scroll bar    /// &lt;/summary&gt;
    /// &lt;param name="tree"&gt;TreeList&lt;/param&gt;
    public static void HorzScroll(this TreeList tree)
    {
      if (tree == null)
        throw new ArgumentNullException("tree");
       = false;
      ();
       = ;
    }
    /// &lt;summary&gt;
    ///Add a right-click menu to TreeList    /// Called in MouseUp(object sender, MouseEventArgs e) event    /// &lt;/summary&gt;
    /// &lt;param name="tree"&gt;TreeList&lt;/param&gt;
    /// &lt;param name="e"&gt;MouseEventArgs&lt;/param&gt;
    /// &lt;param name="menu"&gt;PopupMenu&lt;/param&gt;
    /// &lt;param name="attachMenuRule"&gt;AttachMenuRule&lt;/param&gt;
    public static void AttachMenu(this TreeList tree, MouseEventArgs e, PopupMenu menu, Func&lt;TreeListNode, bool&gt; attachMenuRule)
    {
      if (tree == null)
        throw new ArgumentNullException("tree");
      if (menu == null)
        throw new ArgumentNullException("menu");
      if ( ==  &amp;&amp;  ==  &amp;&amp;  == )
      {
        Point _point = new Point(, );
        TreeListHitInfo _hitInfo = ();
        if (_hitInfo.HitInfoType == )
          (_hitInfo.Node);
        if (attachMenuRule())
          (_point);
      }
    }
    /// &lt;summary&gt;
    /// Set the status of the parent node AfterCheckNode(object sender, NodeEventArgs e)    /// &lt;/summary&gt;
    /// &lt;param name="node"&gt;&lt;/param&gt;
    /// &lt;param name="check"&gt;&lt;/param&gt;
    public static void ProcessNodeCheckState(this TreeListNode node, CheckState check)
    {
      if (node == null)
        throw new ArgumentNullException("node");
      SetCheckedChildNodes(node, check);
      SetCheckedParentNodes(node, check);
    }
    /// &lt;summary&gt;
    /// Set child node CheckState    /// &lt;/summary&gt;
    /// &lt;param name="node"&gt;&lt;/param&gt;
    /// &lt;param name="check"&gt;&lt;/param&gt;
    private static void SetCheckedChildNodes(TreeListNode node, CheckState check)
    {
      if (node != null)
      {
        ((TreeListNode _node) =&gt;
        {
          _node.CheckState = check;
        });
      }
    }
    /// &lt;summary&gt;
    /// Set parent node CheckState    /// &lt;/summary&gt;
    /// &lt;param name="node"&gt;&lt;/param&gt;
    /// &lt;param name="check"&gt;&lt;/param&gt;
    private static void SetCheckedParentNodes(TreeListNode node, CheckState check)
    {
      if ( != null)
      {
        bool _checkStatus = false;
        CheckState _nodeState;
        node.LoopTreeNodes_Break((TreeListNode _node) =&gt;
        {
          _nodeState = _node.CheckState;
          if (!(_nodeState))
          {
            _checkStatus = !_checkStatus;
            return false;//Breaking out of the loop          }
          return true;//Continue the loop        });
         = _checkStatus ?  : check;
        SetCheckedParentNodes(, check);
      }
    }
    /// &lt;summary&gt;
    /// Get TreeListNode according to CheckState    /// &lt;/summary&gt;
    /// &lt;param name="tree"&gt;TreeList&lt;/param&gt;
    /// &lt;param name="state"&gt;CheckState&lt;/param&gt;
    /// <param name="GetNodesByStateRule">Continue when returning to True</param>    /// <returns>TreeListNode collection</returns>    public static List&lt;TreeListNode&gt; GetNodesByState(this TreeList tree, CheckState state, Func&lt;TreeListNode, bool&gt; GetNodesByStateRule)
    {
      if (tree == null)
        throw new ArgumentNullException("tree");
      List&lt;TreeListNode&gt; _checkNodes = new List&lt;TreeListNode&gt;();
      ((TreeListNode node) =&gt;
      {
        if (GetNodesByStateRule(node))
        {
          if ( == state)
            _checkNodes.Add(node);
        }
      });
      return _checkNodes;
    }
  }
}

The examples in this article are detailed and detailed notes that can help you better understand.