Click here to Skip to main content
12,511,553 members (46,647 online)
Click here to Skip to main content
Add your own
alternative version

Stats

63K views
1.5K downloads
18 bookmarked
Posted

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>

 

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionCan you attach a sample project? Pin
mdunn621-Aug-06 6:32
membermdunn621-Aug-06 6:32 
AnswerRe: Can you attach a sample project? Pin
SULMAN SARWAR21-Aug-06 20:10
memberSULMAN SARWAR21-Aug-06 20:10 
QuestionWhat's this?? Pin
AxelM21-Mar-06 0:51
memberAxelM21-Mar-06 0:51 
GeneralMissing Pin
The_Myth20-Mar-06 23:55
memberThe_Myth20-Mar-06 23:55 
GeneralRe: Missing Pin
SALMAN SARWAR21-Mar-06 1:11
memberSALMAN SARWAR21-Mar-06 1:11 
GeneralRe: Missing Pin
The_Myth21-Mar-06 1:59
memberThe_Myth21-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160929.1 | Last Updated 22 Aug 2006
Article Copyright 2006 by SULMAN SARWAR
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid