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

后端 未结 8 1688
故里飘歌
故里飘歌 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:31

    This code help you to iterate though all TreeView list with identifying current depth level. Code can be used to save TreeView items to XML file and other purposes.

    int _level = 0;
            TreeNode _currentNode = treeView1.Nodes[0];
            do
            {
                MessageBox.Show(_currentNode.Text + " " + _level);
                if (_currentNode.Nodes.Count > 0)
                {
                    _currentNode = _currentNode.Nodes[0];
                    _level++;
                }
                else
                {
                    if (_currentNode.NextNode != null)
                        _currentNode = _currentNode.NextNode;
                    else
                    {
                        _currentNode = _currentNode.Parent.NextNode;
                        _level--;
                    }
                }
            }
            while (_level > 0);
    

提交回复
热议问题