Skip to main content

Treeview Structure in VB.Net

In this post, we will look at how the Treeview can be used.

I am using Visual Basic 2008 Express Edition.

In one of my earlier posts, I had explained in detail about Treeview in Excel-VBA. Through this post, I would like to bring the VB.Net way of the control.

1) get the Control:

From the Toolbox (Common Controls tab), drag the treeview onto your form.
For convenience, I have named it - TV.

2) Build the Tree up:

You build tree by adding nodes as child of another Node (or as a child of the TV).

Special things to note for a Node.

You can specify an image for a Node.

You can attach an Object to each Node through the TAG property of the Node. This is something which I found really useful.

Node Object : TreeNode

Adding a node:

You can add a node to the TV.

'--- Start ---
Dim iNode As New TreeNode
Dim iObj as New MyObject

'Define iObj...

iNode.Name = "Name1"
iNode.Text = "Root Node 1"
iNode.Tag = iObj


'--- End ---

Or You can add a node to an existing Node.

'--- Start ---
Dim iNode As New TreeNode
Dim pNode as TreeNode
Dim iObj as New MyObject

'Define iObj...

pNode = TV.Nodes(0) 'Zero is the index of the Node at that Level.

iNode.Name = "Name2"
iNode.Text = "Child Node 1"
iNode.Tag = iObj


'--- End ---

Removing a node:



iNode = TV.SelectedNode 'or Get the Node of the TV as an Object.

Clearing all nodes:


3. Usual properties of Nodes:

You can refer to a node with its Index.

'Gets or sets the Text

'Gets or sets the Name of the Node

'say iNode = TV.Node(0).Nodes(2)

'Gets the Index of the Node at that Level.

'Gets the No of children the has got.

'Gets the First Child Node and the Last Child Node


more, later some time...


Popular posts from this blog

TreeView Structure in Excel VBA

TreeView is extreamly useful in specific cases but it can be bit tricky sometimes to implement.  Last Few days I was working on a TreeView Structure and thought I will share the knowledge I gained...  This post takes you through the basic operations to create and operate a TreeView. It will be like the Folder tree window of the windows explorer.  Moreover, you can have it dynamically updated based on the data in excel.... Here we go..

Excel Function :TABLE

Its been a while since I have done some posting. This option in excel is something which I thought would be very useful for many. Its the TABLE Function in MS Excel. This is especially useful in the following scenarios.