TreeView in C#

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;