Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

c# - Accessing all the nodes in TreeView Control

I have a TreeView Control with set of nodes and child nodes. For example:

ROOT has A,B,C.

A has a1, a2, a3 and then that a1, a2 also contains some nodes like x1, x2, x3 and so on. Like this many subnodes are there. I know it is possible to use loops with a for loop.

I just want to access all the nodes in TreeView control using one or two for loops.

Is there any algorithm for that or is there any other way?

One more question: Is it is possible to have the path of a tree node in an object or in a string using any library functions? For example:

string S = TreeView1.Nodes[i].Nodes[j].Nodes
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't use nested loops, but go for an recursive solution like:

void ListNodes( TreeNode node )
{
  foreach( var subnode in node.Nodes )
  {
    ListNodes( subnode );
  }
  // Print out node
}

Call this function for your root node.

For your additional question: check the FullPath property.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...