SIGN UP MEMBER LOGIN:    
ARTICLE

TreeView in C#

Posted by Prasad H Articles | Windows Controls C# February 05, 2002
This program shows you how to use TreeView control in Windows forms using C# including adding, removing and searching nodes.
Reader Level:
Download Files:
 

Understanding TreeView Control

TreeView Control is used for showing hierarchical structured data visually eg. XML.

Class Hierarchy of TreeView Control

  • TreeView
  • TreeNodeCollection
  • TreeNode

TreeView is contains collection of TreeNodes. Each TreeNode is a member of TreeNodeCollection. 

TreeNode is a type of recursive Data Structure as itself contains Collection of TreeNodes. All TreeNodes are number from 0 to Nodes.Count-1 sequentially in the order of appearance irrespective of the hierarchical structure

Sample:

ROOT
T1
T11
T2


For the above structure the nodes are numbered as 

ROOT - 1
T1 - 2
T11 - 3
T2 - 4

1. Creating TreeView

TreeView is created by instancing the TreeView class.

Sample:

TreeView tvSample = new TreeView();

2. Add Nodes to TreeView

The Nodes property of the TreeView is an instance of TreeNodeCollection class which is used to Add/Remove Nodes.

Sample:

tvSample.Nodes.Add("Sample") 

or

tvSample.Nodes.Add(new TreeNode("Sample"))

3. Removing Nodes from TreeView

The Nodes Property of the TreeView is an instance of TreeNodeCollection class which is used to Add/Remove Nodes.

Sample:

tvSample.Nodes.RemoveAt(1) //To Remove the First appearing node in the Tree.

or

tvSample.Nodes.Remove(tvSample.SelectedNode) //Removes the currently selected node.

4. Determine the SelectedNode

SelectedNode property is used to determine the currently selected node in the Tree.

Sample:

string nodeText=tvSample.SelectedNode.Text;

will retrieve the Text of the selected node.

Other Useful Members of TreeView

TreeView::Indent

Space between Parent and child nodes.

TreeView::ItemHeight

Height of each TreeNode in the Tree.

TreeNode::FullPath

Returns the Path of the Node from the its Topmost Parent.

Sample:

ROOT
T1
T11
T2

Fullpath of T2 is ROOT/T1/T11/T2

TreeNode::Expand()
Expand the currently selected node.

TreeNode::ExpandAll()
Expand the currently selected node and all its child hierarchy.

TreeNode::Collapse()
Collapse the currently selected node.

TreeNode::ImageIndex
Showing an image in TreeNode.To use this property.The TreeView's ImageList
property is set to an ImageList.


Useful Tips

[Q] How do I show all the Nodes in the TreeView?

[A] tvSample.SelectedNode=tvSample.Nodes[0]
tvSample.SelectedNode.ExpandAll()

[Q] How do I collapse all the parent Nodes in the TreeView?
[A] It's not possible directly but can be done using coding.

void CollapseTree(TreeNode ParentNode)
{
if(ParentNode.IsExpanded)
ParentNode.Toggle();
for(int i=ParentNode.Nodes.Count-1;i>=0;i--)
{
if(ParentNode.Nodes[i].Nodes.Count>0)
{
if(ParentNode.Nodes[i].IsExpanded)
ParentNode.Nodes[i].Toggle();
CollapseTree(ParentNode.Nodes[i])
}
}
}

[Q] How do I make the NodeText as Hyperlink?
[A] tvSample.HotTracking=true;

[Q] Can I have Checkboxes in TreeNode?
[A]tvSample.CheckBoxes=true;

[Q] How do I show all the Nodes in the TreeView?
[A] tvSample.SelectedNode=tvSample.Nodes[0]
tvSample.SelectedNode.ExpandAll()

[Q] How do I collapse all the parent Nodes in the TreeView?
[A] It's not possible directly but can be done using coding.

void CollapseTree(TreeNode ParentNode)
{
if(ParentNode.IsExpanded)
ParentNode.Toggle();
for(int i=ParentNode.Nodes.Count-1;i>=0;i--)
{
if(ParentNode.Nodes[i].Nodes.Count>0)
{
if(ParentNode.Nodes[i].IsExpanded)
ParentNode.Nodes[i].Toggle();
CollapseTree(ParentNode.Nodes[i])
}
}
}

[Q] How do I make the NodeText as Hyperlink?
[A] tvSample.HotTracking=true;

[Q] Can I have Checkboxes in TreeNode?
[A]tvSample.CheckBoxes=true;

Login to add your contents and source code to this article
share this article :
post comment
 
Posted by Pradeep Kumar May 23, 2010

esfw

Posted by hamid hemati Apr 07, 2010

Hi thanks for the post.

I want to learn that how can i show count of all nodes of treeview ?

for example i will click buton and it will show at label the count of all nodes of treview.

Posted by Gokhan Guven Nov 12, 2009

needs forms in download to complete the example

Posted by Tedd Sandstrom Sep 08, 2008
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor