Click here to Skip to main content
Click here to Skip to main content
Go to top

C# TreeView Traversing

, 21 Aug 2006
Rate this:
Please Sign up or sign in to vote.
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


<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)

{
//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
} 

 

private void visitChildNodes(TreeNode node)


{
//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

Share

About the Author

SULMAN SARWAR
Web Developer
Pakistan Pakistan
No Biography provided

Comments and Discussions

 
QuestionCan you attach a sample project? Pinmembermdunn621-Aug-06 6:32 
AnswerRe: Can you attach a sample project? PinmemberSULMAN SARWAR21-Aug-06 20:10 
QuestionWhat's this?? PinmemberAxelM21-Mar-06 0:51 
GeneralMissing PinmemberThe_Myth20-Mar-06 23:55 
GeneralRe: Missing PinmemberSALMAN SARWAR21-Mar-06 1:11 
GeneralRe: Missing PinmemberThe_Myth21-Mar-06 1:59 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 22 Aug 2006
Article Copyright 2006 by SULMAN SARWAR
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid