Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / Windows Forms
Article

C# TreeView Traversing

Rate me:
Please Sign up or sign in to vote.
2.75/5 (23 votes)
21 Aug 2006 74.9K   1.6K   18   6
This article presents a simple c# TreeView traversal mechanism.

Introduction

TreeView in C# is a great control. TreeView can be used to build Hierarchical Menus. I was involved in a project where i had to make use of TreeView quite heavily and as a result i wrote some code myself to Build the TreeView dynamically (database driven). Here i present the TreeView traversal operation.

Here's a snapshot of the Populated TreeView.

Download sourcecode

Sample screenshot

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Here is the code:

class</FONT> TVIEW


C#
<P>

<FONT color=#008000 size=1></P><P>//1- Create a TreeView</P></FONT><FONT size=1><P>TreeView treeview = </FONT><FONT color=#0000ff size=1>new</FONT><FONT size=1> TreeView();
</FONT><FONT size=1></P><P></FONT><FONT color=#008000 size=1></FONT> </P><P><FONT color=#008000 size=1>//2- Populate the TreeView with Data</P></FONT><FONT size=1><P></FONT><FONT color=#008000 size=1>//populateTreeView();
</P></FONT><P><FONT color=#008000 size=1>//3- Now You can visit each and every node of this treeview whenever you require</P></FONT><FONT size=1><P>TVIEW t1 = </FONT><FONT color=#0000ff size=1>new</FONT><FONT size=1> TVIEW();</P><P>t1.TraverseTreeView(treeview);</P><P></FONT>

</P>

 

private void TraverseTreeView(TreeView tview)<BR>

C#
{
//Create a TreeNode to hold the Parent Node
TreeNode temp = new TreeNode();

//Loop through the Parent Nodes
for(int k=0; k<tview.Nodes.Count; k++) 
{
//Store the Parent Node in temp
temp = tview.Nodes[k];

//Display the Text of the Parent Node i.e. temp
MessageBox.Show(temp.Text);

//Now Loop through each of the child nodes in this parent node i.e.temp
for (int i = 0; i < temp.Nodes.Count; i++)
visitChildNodes(temp.Nodes[i]); //send every child to the function for further traversal
} 

 </P>
<P><FONT color=#0000ff size=1>private</FONT><FONT size=1> </FONT><FONT color=#0000ff size=1>void</FONT><FONT size=1> visitChildNodes(TreeNode node)</FONT></P><PRE lang=cs>
{ //Display the Text of the node

MessageBox.Show(node.Text);

//Loop Through this node and its childs recursively

for (int j = 0; j < node.Nodes.Count; j++)

visitChildNodes(node.Nodes[j]);

}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCan you attach a sample project? Pin
mdunn621-Aug-06 6:32
mdunn621-Aug-06 6:32 
AnswerRe: Can you attach a sample project? Pin
SULMAN SARWAR21-Aug-06 20:10
SULMAN SARWAR21-Aug-06 20:10 
QuestionWhat's this?? Pin
AxelM21-Mar-06 0:51
AxelM21-Mar-06 0:51 
GeneralMissing Pin
AnasHashki20-Mar-06 23:55
AnasHashki20-Mar-06 23:55 
GeneralRe: Missing Pin
SULMAN SARWAR21-Mar-06 1:11
SULMAN SARWAR21-Mar-06 1:11 
GeneralRe: Missing Pin
AnasHashki21-Mar-06 1:59
AnasHashki21-Mar-06 1:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.