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
TV.Nodes.Add(iNode)
'--- 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
pNode.Nodes.Add(iNode)
'--- End ---
Removing a node:
TV.Nodes(0).Nodes(0).Remove()
or
iNode = TV.SelectedNode 'or Get the Node of the TV as an Object.
iNode.Remove()
Clearing all nodes:
TV.Nodes.Clear()
3. Usual properties of Nodes:
You can refer to a node with its Index.
'Gets or sets the Text
TV.Nodes(i).Text
'Gets or sets the Name of the Node
TV.Nodes(i).Name
'say iNode = TV.Node(0).Nodes(2)
'Gets the Index of the Node at that Level.
iNode.Index
'Gets the No of children the has got.
iNode.Nodes.Count
'Gets the First Child Node and the Last Child Node
iNode.FirstNode
iNode.LastNode
etc....
more, later some time...
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
TV.Nodes.Add(iNode)
'--- 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
pNode.Nodes.Add(iNode)
'--- End ---
Removing a node:
TV.Nodes(0).Nodes(0).Remove()
or
iNode = TV.SelectedNode 'or Get the Node of the TV as an Object.
iNode.Remove()
Clearing all nodes:
TV.Nodes.Clear()
3. Usual properties of Nodes:
You can refer to a node with its Index.
'Gets or sets the Text
TV.Nodes(i).Text
'Gets or sets the Name of the Node
TV.Nodes(i).Name
'say iNode = TV.Node(0).Nodes(2)
'Gets the Index of the Node at that Level.
iNode.Index
'Gets the No of children the has got.
iNode.Nodes.Count
'Gets the First Child Node and the Last Child Node
iNode.FirstNode
iNode.LastNode
etc....
more, later some time...
Comments