Get a list of all tree nodes (in all levels) in TreeView Controls

后端 未结 8 1670
故里飘歌
故里飘歌 2020-12-06 11:53

How can I get a list of all tree nodes (in all levels) in a TreeView control?

8条回答
  •  自闭症患者
    2020-12-06 12:06

    You can use two recursive extension methods. You can either call myTreeView.GetAllNodes() or myTreeNode.GetAllNodes():

    public static List GetAllNodes(this TreeView _self)
    {
        List result = new List();
        foreach (TreeNode child in _self.Nodes)
        {
            result.AddRange(child.GetAllNodes());
        }
        return result;
    }
    
    public static List GetAllNodes(this TreeNode _self)
    {
        List result = new List();
        result.Add(_self);
        foreach (TreeNode child in _self.Nodes)
        {
            result.AddRange(child.GetAllNodes());
        }
        return result;
    }
    

提交回复
热议问题