Re: Treeview help
- From: "MikeD" <nobody@xxxxxxxxxxx>
- Date: Sat, 24 Dec 2005 20:01:57 -0500
<Fern G> wrote in message news:OGg4ZGOCGHA.1676@xxxxxxxxxxxxxxxxxxxxxxx
> Mike,
>
> Thank you for your response. Can you please show me how I add
> nodes to a tree node, and the rest I will look up, because I am
> really having difficulty with this, I am not just being lazy, I really
> am having problems. It shouldn't be to difficult to correct my
> code. But I also need the following feature:
Here's an example that adds 2 root nodes, 2 child nodes, and 2 "grandchild"
nodes. You can easily add any number of nodes to any level just by
specifying a different number for each For..Next loop's ending value. Note
that root nodes are bold, child nodes are red, and grandchildren are green.
Just thought I'd throw that extra little bit in. Note that this requires the
VB6 version of Windows Common Controls because the VB5 version doesn't have
the Bold and ForeColor properties for Node objects (you can do the same
thing for the VB5 version using the Win32API, though). If you're using the
VB5 version, just comment out (or remove) the lines containing .Bold and
..ForeColor. I also added some code in the NodeClick event to demonstrate
what the Children property does since you were confused about that property
too.
-----BEGIN CODE
Private Sub Form_Click()
Dim nodRoot As Node
Dim nodChild As Node
Dim nodGrandchild As Node
Dim lRoot As Long
Dim lChild As Long
Dim lGrandchild As Long
With TreeView1.Nodes
For lRoot = 1 To 2
Set nodRoot = .Add(, , "Root " & CStr(lRoot), "Root " &
CStr(lRoot))
nodRoot.Bold = True
nodRoot.Expanded = True
For lChild = 1 To 2
Set nodChild = .Add(nodRoot.Key, tvwChild, nodRoot.Key &
"Child " & CStr(lChild), "Child " & CStr(lChild))
nodChild.ForeColor = vbRed
nodChild.Expanded = True
For lGrandchild = 1 To 2
Set nodGrandchild = .Add(nodChild.Key, tvwChild,
nodChild.Key & "Grandchild " & CStr(lGrandchild), "Grandchild " &
CStr(lGrandchild))
nodGrandchild.ForeColor = vbGreen
Next
Next
Next
End With
End Sub
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
MsgBox Node.FullPath & " has " & CStr(Node.Children) & " child nodes"
End Sub
-----END CODE
I think this example demonstrates the basics of what you need to know.
Study it until you understand how it works and why it does what it does.
Press F8 to step through it line by line. For best results, make your form
window and treeview control fairly small and position the code window so
that you can see the complete form and the code window at the same time.
That way, as you're stepping through the code, you can see exactly what's
happening in the form.
>
> If a cerain node already exists, I musn't add it twice ( because
> a path must be unique).
Use keys for each of your nodes. If you try to add a node using a key that
already exists, you'll get an error (the error number is 35602) that you
should trap. Write error handling code to deal with it however you need to.
Note that in the example above, the key for each child and grandchild nodes
are derived from the parent's key. Now while you don't have to use that same
strategy, I personally find it to be a pretty good one. Since you're wanting
to have folder names for your nodes, this strategy for keys should work
quite well for you because you can easily get a reference to any node if you
know the path (specified by a user, maybe assigned to a textbox, or
whatever). IOW, the key of each node would be the folder's path.
>
> If you are absolutely sure VB does explain this, can you post the
> exact url (in msdn6 format)
It documents what you need to know. It doesn't necessarily tell you how to
do exactly what you're wanting to do. It gives you the information you need
to know in order to do it.
In the IDE, position the cursor within the word Add (for a Nodes collection)
and press F1. You'll go right to the documentation for the Add method of
the Nodes collection. If you don't have MSDN Library installed on your PC,
then install it. If you don't have it to install (no reason you shouldn't
unless you lost the CD/DVD or your copy of VB6 is pirated), then you can use
the online version of MSDN Library. Here's the URL for that:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cmctl198/html/vbmthaddnode.asp
--
Mike
Microsoft MVP Visual Basic
.
- Follow-Ups:
- Re: Treeview help
- From: Fern G
- Re: Treeview help
- From: Fern G
- Re: Treeview help
- References:
- Treeview help
- From: Fern G
- Re: Treeview help
- From: MikeD
- Re: Treeview help
- From: Fern G
- Treeview help
- Prev by Date: Re: Textbox Question
- Next by Date: Re: Treeview help
- Previous by thread: Re: Treeview help
- Next by thread: Re: Treeview help
- Index(es):
Relevant Pages
|