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

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

Popular posts from this blog

PTC Creo | my Mapkeys for free

I have created a list of frequently used mapkey shortcuts for the PTC Pro Engineer Creo. This is the macro equivalent in creo.  I am copying them below.If you need them, copy paste the content to the "config.pro" file in your startup folder. My favourites are highlited and greatly improves the workflow.. For ex, to reach MEASURE. need to go to another menu and click.  Instead, maypkey from any selected menu on the ribbon will work.. Thats wonderful to me... Also, Edit Sketch (ES) is overloaded and will work for Extrusion, Revelution, Sweep etc.. So is aa, pp, zz..  really helps me a lot.. Hope you will start using them as well and get benefited! Let me know in comments, your feedback and issues.... Sketch View           > sv Show and Erase        > se Working Directory     > wd Hiddel Line View      > hi Close (quit) Window   > qw Measure        ...

Color the Excel row based on a value/status

Very often, we would like to differenciate the rows that are having certain status (or specific values in a specific column) in a different color. We can filter for the status and apply the color manually, but we can also do that dynamically or automatically with a specific tweak to the Conditional Formatting feature in Excel