Saturday, 29 December 2007

TreeView Structure in Excel VBA


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.

1. Get the additional control for the TreeView:

Get to the VBA Window, Right Click on the ToolBox Window and Select Additional Controls... --- Browse through the window and select "Microsoft TreeView Control ...." and "Microsoft ImageList Control ....". --- Now these two items would have come in your TooBox. --- Select and place it on the Form and start working.

2. Name the Tree:

I have changed the name of the tree to TV from the default TreeView1. Hereon, I will use TV for that item.

3. Build the Tree up:

You build tree by adding nodes. You can add a node as a Child or a Parent etc.. I like to use the relationship - Child.

Basically, each node has a Key (like the name of controls), Text (like the Caption of controls), Index (the ListIndex of a ListBox)

Adding Node

You create each node in relation to another.
TV.Nodes.Add(Relative:="ThekeyofParent", Relationship:=tvwChild, Key:="NewKey", Text:="New Text")

The base Node can be added as..
TV.Nodes.Add( , , "Key001", "The Base")

The Next Node can be added as..
TV.Nodes.Add("Key001", tvwChild, "Key011", "The First Child")

Removing Node

Remove the Node by refering to the Key or the Index of the item.

Clear the Nodes


4. Usual properties of Nodes:

You can refer to a node with its Key or Index.

TV.Nodes(i).Text or simply TV.Nodes(i)
TV.Nodes(i).Key ' Gives the Key of the Node.
TV.Nodes(key).Index ' Gives the Index of the the Node.
TV.Nodes(key).children ' Gives the No of children it has.

Its safe to use Int(i) and str(Key) when you are using other variable to refer to index or key.

5. Nodes Handling:

Defining the variable for assigning Nodes

Dim xNode As MSComctlLib.Node

Usual ways of assigning a node to the variable..

Set xNode = TV.Nodes(i)
Set xNode = TV.Nodes(key)
Set xNode = TV.SelectedItem

Get a parent Node or Child Node and cycle through the Nodes...

To assign xNode to the Parent of the selected item..
Set xNode = TV.SelectedItem.Parent

To assign xNode to the First Child of the selected item..
Set xNode = TV.SelectedItem.Child

To assign xNode to the Parent of the item..
Set xNode = xNode.Parent

To assign xNode to the First Child of the item..
Set xNode = xNode.Child

To assign xNode to the Next item of the same level and same parent
Set xNode = xNode.Next

To assign xNode to the First item of the same level and same parent
Set xNode = xNode.FirstSibling

To assign xNode to the Last item of the same level and same parent
Set xNode = xNode.LastSibling

Remember to Check the node existance before start using it..

Check if Node is selected or assigned properly..

If Node Is Nothing Then
MsgBox "Node not Selected or nonexistant!"
Exit Sub
End If

Further Reading :


Thursday, 20 December 2007

Tip on creating Co-ordinate system faster.

Target : Proe Wildfire 2.0
Topic : Datum Creation

Have your ever wanted to make a co-ordinate system to assemble a component, but its too complex. Its easy! Here we goooo...

Saturday, 1 December 2007


Hi all,

I was thinking about creating such a post put in paper some of things that I think would be use full for at least some...

Keep watching this space..