chitika

Friday, September 16, 2011

vb: how to use treeview and child node?

This site provides users with the information about vb, visual basic, vb6, use treeview, how to use treeview, treeview node, child node, how to add child node, example, and more.

If you think that this site is helpful, please recommend your friends to visit our site.

How to use treeview and child node in vb?

The TreeView control has a lot of options, but here is the basics of TreeView to get you started. The following is the syntax for adding a node:

TreeView1.Nodes.Add Relative, Relationship, Key, Text, Image, SelectedImage

You should be clear that all of the above parameters are optional, but you should know where the new item goes by using Relative and Relationship. Relative defines a relation, or an existing node, to the new node you are creating, while Relationship defines how the new node is related to this existing node. The following is a little reference of relationships for you:

tvwChild: new node is a child (one level down) of the specified node
tvwFirst: new node is first, at the same level of the specified node
tvwLast: new node is last, at the same level of the specified node
tvwNext: new node is after (at the same level) the specified node
tvwPrevious: node is before (at the same level) the specified node

You should also note that a TreeView gets its images from an ImageList, that is, if you want to add images, you need to add a ImageList control. The following is a little reference for ImagList:

1. In design mode, draw ImageList icon onto your Form.
2. Right-click on the ImageList and select Properties.
3. Choose the Images property page.
4. Click the Insert Picture button.
5. After selecting a picture file, add a unique key into the Key field.
6. Use the above syntax to add images.

The following is the code, example, steps for creating a TreeView in vb:

--------------------------------------------------------------------------------
1. In Component, add Microsoft Windows Common Controls 6.0
2. Go to design mode.
3. After seeing TreeView on Toolbox, drag the TreeView onto your form.
4. Once you have the TreeView, you can populate it in programming.
Add the following code in form_load.

Private Sub Form_Load()

TreeView1.Nodes.Add , , "root", "Root"
TreeView1.Nodes.Add "root", tvwChild, "child1", "Child1"
TreeView1.Nodes.Add "root", tvwChild, "child2", "Child2"
TreeView1.Nodes.Add "child1", tvwChild, "child_child1", "Child of Child1"
TreeView1.Nodes.Add "child2", tvwChild, "child_child2", "Child of Child2"

'add the following if you want Child1 of Child1 and Child1 of Child2 visible
TreeView1.Nodes("child_child1").EnsureVisible
TreeView1.Nodes("child_child2").EnsureVisible

End Sub

5. Add the following code in NodeClick if you want to perform
a specific task when you click it.

Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
If Node.Text = "Child of Child1" Then
MsgBox "Child of Child1"
ElseIf Node.Text = "Child of Child2" Then
MsgBox "Child of Child2"
End If
End Sub
--------------------------------------------------------------------------------

No comments:

Post a Comment

For other hundreds of computer repair tips, tricks, tweaks, guide, help, please go to


http://website.edusoftmax.com