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
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)
{
TreeNode temp = new TreeNode();
for(int k=0; k<tview.Nodes.Count; k++)
{
temp = tview.Nodes[k];
MessageBox.Show(temp.Text);
for (int i = 0; i < temp.Nodes.Count; i++)
visitChildNodes(temp.Nodes[i]); }
private void visitChildNodes(TreeNode node)
{
//Display the Text of the nodeMessageBox.Show(node.Text);
//Loop Through this node and its childs recursivelyfor (int j = 0; j < node.Nodes.Count; j++)visitChildNodes(node.Nodes[j]);
}
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