For Programmers: Free Programming Magazines  


Home > Archive > Visual Basic > January 2006 > Treeview help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Treeview help

2006-01-09, 7:16 pm

Can someone help me, I need to create a tree view from
a file path ( like the folder view of Explorer )
I have all the path components in a string array
s() which is obtained using 'split(path,"\")'

I tried this

While i < UBound(s1)
Set mynodes = mynod.Children
Set mynod = mynodes.Add(, , s(i), s(i))
i = i + 1
Wend

but I think the children property does not actually return a nodes of a
node.
Any ideas?


MikeD

2006-01-09, 7:16 pm


<Fern G> wrote in message news:%23wewxjMCGHA.2644@TK2MSFTNGP09.phx.gbl...
> Can someone help me, I need to create a tree view from
> a file path ( like the folder view of Explorer )
> I have all the path components in a string array
> s() which is obtained using 'split(path,"\")'
>
> I tried this
>
> While i < UBound(s1)
> Set mynodes = mynod.Children
> Set mynod = mynodes.Add(, , s(i), s(i))
> i = i + 1
> Wend
>
> but I think the children property does not actually return a nodes of a
> node.



No, it doesn't. If returns the number of children the node has.

You set a node's relationship with another node when you add the node. This
is done by specifying values for the first 2 arguments of the Add method.

Spend some time reading VB's Help. It explains everything you need to know
to do this.

--
Mike
Microsoft MVP Visual Basic





2006-01-09, 7:16 pm

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:

If a cerain node already exists, I musn't add it twice ( because
a path must be unique).

If you are absolutely sure VB does explain this, can you post the
exact url (in msdn6 format)

tia.


MikeD

2006-01-09, 7:16 pm


<Fern G> wrote in message news:OGg4ZGOCGHA.1676@TK2MSFTNGP09.phx.gbl...
> 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 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/d...bmthaddnode.asp


--
Mike
Microsoft MVP Visual Basic


2006-01-09, 7:16 pm

Mike, thank you,

there is a lot to absorb in what you posted. Can I just ask you something
quickly before I read your post? In vb.net it's possible to get a nodes
collection from a node like this

nodes= node.nodes

Is this possible also in VB6.

The problem as I see it is that initially I have the root collection, i.e.
treeview1.
Then, I need to add by key reference a new root node, and then obtain that
node's collection, and so on. Is this the correct approad?

o.k. now I am going to read your post

t.


2006-01-09, 7:16 pm

Hi Mike,

I use this

Set mynod = TreeView1.Nodes.Add(mynod.Key, tvwChild, s1(i), s1(i))

to add my nodes. Do you know how I can obtain 'mynod' when there
is a duplicate key, since the above will be an error, hence I need to
set the node, without adding it.

tia


MikeD

2006-01-09, 7:16 pm


<Fern G> wrote in message news:e$bxhVPCGHA.1312@TK2MSFTNGP09.phx.gbl...
> Mike, thank you,
>
> there is a lot to absorb in what you posted. Can I just ask you something
> quickly before I read your post? In vb.net it's possible to get a nodes
> collection from a node like this
>
> nodes= node.nodes
>
> Is this possible also in VB6.


No. In VB6's Treeview, there's only one Nodes collection, a reference to
which is obtained from the Nodes property of the Treeview control itself. In
VB.NET, each TreeNode object has its own TreeNodeCollection. I supposed you
could write a wrapper class for VB6's Treeview to provide this
functionality, though.

>
> The problem as I see it is that initially I have the root collection, i.e.
> treeview1.
> Then, I need to add by key reference a new root node, and then obtain that
> node's collection, and so on. Is this the correct approad?


No. As I said, there's only *one* Nodes collection. It includes ALL Node
objects for the entire Treeview control. There are several properties for
getting references to related Node objects. Look up the Child, FirstSibling,
LastSibling, Next, Previous, Parent, and Root properties of a Node object.
All of these return a reference to some other Node object.

It sounds like you're coming from a VB.NET background. You need to quit
thinking in VB.NET terms when working in VB6. <g>


> o.k. now I am going to read your post


You probably should have read it and tried the sample code first. It might
have answered your questions.

--
Mike
Microsoft MVP Visual Basic


MikeD

2006-01-09, 7:16 pm


<Fern G> wrote in message news:ubx6w6WCGHA.916@TK2MSFTNGP10.phx.gbl...
> Hi Mike,
>
> I use this
>
> Set mynod = TreeView1.Nodes.Add(mynod.Key, tvwChild, s1(i), s1(i))
>
> to add my nodes. Do you know how I can obtain 'mynod' when there
> is a duplicate key, since the above will be an error, hence I need to
> set the node, without adding it.


Well, since you're getting a duplicate key error, you know that a node
having that key must already exist. So, just use that key to get a reference
to the node from the Nodes collection. Here's an example (air code):


On Error GoTo EH

Set mynod = TreeView1.Nodes.Add(mynod.Key, tvwChild, s1(i), s1(i))
mynod.Bold = Not mynod.Bold

Exit Sub

EH:

If Err.Number = 35602 Then 'duplicate key error number
Set mynod = Treeview1.Nodes(mynod.Key)
Resume Next
Else
MsgBox Err.Description
End If

End Sub

Note that mynod must already be assigned a reference to a Node object
(otherwise, you'll get an 'object variable or with block variable not set'
error). I was just building on the line of code you posted.

Here's tested code that's a better example:

Private Sub Form_Load()

Dim mynod As Node

On Error GoTo EH

Set mynod = TreeView1.Nodes.Add(, , "Test", "Test")

'Try to add a duplicate
Set mynod = TreeView1.Nodes.Add(mynod.Key, tvwChild, "Test", "Test")

mynod.Bold = Not mynod.Bold

Exit Sub

EH:

If Err.Number = 35602 Then 'duplicate key error
Set mynod = TreeView1.Nodes(mynod.Key)
Resume Next
Else
MsgBox Err.Description
End If

End Sub


--
Mike
Microsoft MVP Visual Basic


Bob Butler

2006-01-09, 7:16 pm

"MikeD" <nobody@nowhere.edu> wrote in message
news:OsqnrfbCGHA.3920@tk2msftngp13.phx.gbl
> <Fern G> wrote in message news:ubx6w6WCGHA.916@TK2MSFTNGP10.phx.gbl...
> Set mynod = TreeView1.Nodes.Add(mynod.Key, tvwChild, s1(i), s1(i))


As an aside... the first argument is a reference to the parent. You can
pass a key and the control will do a lookup to find the mode but since you
already have the reference it's simpler just to use it:
Set mynod = TreeView1.Nodes.Add(mynod, tvwChild, s1(i), s1(i))

But I'd probably assign the reference to a new variable so that you don't
lose the parent reference if it works.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."

MikeD

2006-01-09, 7:16 pm


"Bob Butler" <tiredofit@nospam.com> wrote in message
news:eFOAA7bCGHA.2036@TK2MSFTNGP14.phx.gbl...
> "MikeD" <nobody@nowhere.edu> wrote in message
> news:OsqnrfbCGHA.3920@tk2msftngp13.phx.gbl
>
> As an aside... the first argument is a reference to the parent. You can
> pass a key and the control will do a lookup to find the mode but since you
> already have the reference it's simpler just to use it:
> Set mynod = TreeView1.Nodes.Add(mynod, tvwChild, s1(i), s1(i))



I didn't know you could pass a reference to a Node object for the first
parameter. I thought it had to be an index or a key (IOW, an integer or a
string). I wonder.....would a reference to a Node object get passed....or
would the default property of the Node object get passed (which I believe is
the Text property)? In any case, I don't think passing a Node object is a
good idea. If nothing else, there's uncertainty (IMO) of what's actually
getting passed. Is it a reference to a Node or is it the default property of
the Node object?

> But I'd probably assign the reference to a new variable so that you don't
> lose the parent reference if it works.


Good advice. If you look back at the example I provided, there are separate
variables for the root nodes, the child nodes, and the grandchild nodes. The
reason behind that is so that you don't lose those references. Of course,
you can easily get a reference to the parent from the Parent property of the
Node (and you can, if necessary, keep on backing up: Node.Parent.Parent).
But that can get confusing.

What matters most is writing clear and concise code. If it means declaring
several object variables, so be it. If you can write clear and concise code
using properties, that's OK too. But except for simple routines, it's not as
likely the code will be as clear as if you used different variables.


--
Mike
Microsoft MVP Visual Basic


Bob Butler

2006-01-09, 7:16 pm

"MikeD" <nobody@nowhere.edu> wrote in message
news:edWVYgcCGHA.2336@TK2MSFTNGP10.phx.gbl
> "Bob Butler" <tiredofit@nospam.com> wrote in message
> news:eFOAA7bCGHA.2036@TK2MSFTNGP14.phx.gbl...
>
> I didn't know you could pass a reference to a Node object for the
> first parameter. I thought it had to be an index or a key (IOW, an
> integer or a string). I wonder.....would a reference to a Node object
> get passed....or would the default property of the Node object get
> passed (which I believe is the Text property)? In any case, I don't
> think passing a Node object is a good idea. If nothing else, there's
> uncertainty (IMO) of what's actually getting passed. Is it a
> reference to a Node or is it the default property of the Node object?


The parameter is defined 'as variant' so what gets passed is the node
reference. I can't see the control determining that it has the reference
and so getting the key to look up the reference again but I guess stranger
things have happened. Unless it can be demonstrated otherwise I'd hold that
passing the reference is the better option.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com