Google Answers Logo
View Question
 
Q: How do I create a treeview ( Answered 4 out of 5 stars,   1 Comment )
Question  
Subject: How do I create a treeview
Category: Computers > Programming
Asked by: jstm-ga
List Price: $50.00
Posted: 04 Mar 2005 23:23 PST
Expires: 04 Apr 2005 00:23 PDT
Question ID: 485027
I need your help. I would like to create a treeview and use it to open
various forms on my Visual Basic 6 application.

I am very new at programming so I need step by step instructions so I
can understand.
Answer  
Subject: Re: How do I create a treeview
Answered By: hammer-ga on 06 Mar 2005 12:00 PST
Rated:4 out of 5 stars
 
Jstm,

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
control.

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"

End Sub

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
        Form1.Show
    ElseIf Node.Text = "Form2" Then
        Form2.Show
    ElseIf Node.Text = "Form3" Then
        Form3.Show
    End If

End Sub
 
Full documentation on the TreeView is available at Microsoft.com.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cmctl198/html/vbobjtreeview.asp

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!

- Hammer


Search Strategy
----------------
None. Created a VB6 project that performs the requested function.

Request for Answer Clarification by jstm-ga on 06 Mar 2005 21:23 PST
You did a great job with this answer and kept it simple for me to
understand. Could you give me a little more to chew on:

-) How can I add pictures to the nodes?

Clarification of Answer by hammer-ga on 07 Mar 2005 09:07 PST
Jstm,

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"

End Sub

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
selected.
TreeView1.Nodes.Add , , , "Form1", "my_icon", "my_other_icon"

Documentation for the Add method for Nodes is available at Microsoft.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cmctl198/html/vbmthaddnode.asp

- Hammer
jstm-ga rated this answer:4 out of 5 stars
Researcher did a great job plus kept it simple so I could understand
(for example: step1, step2, etc.)

Comments  
Subject: Re: How do I create a treeview
From: dreamboat-ga on 05 Mar 2005 10:09 PST
 
There appears to be complete instructions on how to perform this task here:
http://www.vbaccelerator.com/home/VB/Code/Controls/TreeView/TreeView_Shell_Sample/article.asp

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy