The TreeView control has a lot of options, but I've kept this example
very simple and straightforward, since you say you're new at this.
To add a TreeView control to your form, you must first add the
TreeView control to your project. This control is made available by
adding a Component to your project.
1. From the Project menu, select Components.
2. The Components dialog will open. Look at the Controls Tab.
3. The Controls Tab shows a list of available controls. Scroll down
until you find one called Microsoft Windows Common Controls 6.0 (SP6).
Note that you may have to choose a lower version of this depending on
what service pack you have installed.
4. Check the box next to the control name and click Apply. Click OK.
You should now see the TreeView available on the Toolbox. This is the
bar of little pictures of controls that usually appears along the left
side of the Project window. You can now click on the TreeView icon and
draw a TreeView on your Form, just like with any other kind of
Once you have your TreeView, you can populate it in programming when
the Form loads. The code below demonstrates this. Note that, while the
Add method has a number of arguments, I am only using the Text
argument for simplicity's sake. You should use the names of your forms
in place of Form1, Form2, Form3.
Private Sub Form_Load()
TreeView1.Nodes.Add , , , "Form1"
TreeView1.Nodes.Add , , , "Form2"
TreeView1.Nodes.Add , , , "Form3"
Now, your TreeView will display the names of your Forms. To get the
TreeView to open the specified form when the user clicks, you can use
the NodeClick event of the TreeView control. This event runs when a
Node is clicked and knows which Node was clicked. By asking the
clicked Node for its Text property, you can get the name of the Form
you want to open. You should use the names of your forms in place of
Form1, Form2, Form3.
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
If Node.Text = "Form1" Then
ElseIf Node.Text = "Form2" Then
ElseIf Node.Text = "Form3" Then
Full documentation on the TreeView is available at Microsoft.com.
Again, the TreeView has a lot of options, methods and events you can
use, but this example should get you started.
Good luck with your VB project!
None. Created a VB6 project that performs the requested function.
Clarification of Answer by
07 Mar 2005 09:07 PST
A TreeView gets its images from an ImageList. When you added the
reference that got you the TreeView, you also got the ImageList
control. On the Toolbox, it looks like a stack of filecards.
1. Click the ImageList icon and draw one on your Form. It is invisible
in runtime, so size doesn't matter.
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, type a unique key into the Key
field. For this example, I used "my_icon" as a Key.
You now have an image that any control on your Form that knows about
an ImageList can access.
Now, you can change your code to put icons on your tree, like so:
Private Sub Form_Load()
' Tell the TreeView where it's images are.
TreeView1.ImageList = ImageList1
' Make sure we're using a style that
' shows pictures
TreeView1.Style = tvwTreelinesPlusMinusPictureText
' Include the icon in the Add statements
' by using the unique Key as the Image argument
TreeView1.Nodes.Add , , , "Form1", "my_icon"
TreeView1.Nodes.Add , , , "Form2", "my_icon"
TreeView1.Nodes.Add , , , "Form3", "my_icon"
You can put multiple pictures into the ImageList, and use different
ones for each node, if you wish. The Add method also supports setting
one image for a node, and a different image for when the node is
TreeView1.Nodes.Add , , , "Form1", "my_icon", "my_other_icon"
Documentation for the Add method for Nodes is available at Microsoft.