Adding Treenodes

B

Bart Steur

Hi

As a VB6 user I'm unfamiliar with the Treeview control of VB2005. I read it
was changed but I have a hard time finding the right help/samples for my
problem.

In VB6 you had 1 collection, and adding a node was quite simple. Just
specify the nodes key and position relative to the key where you want to add
your new node.
In VB2005 you can't do that anymore. I looking for a sample or help how this
can be done now.
I've no problem adding the first level nodes, but more how directly add a
new node to an existing node referenced by the Key of that node.

I'll hope you understand my problem and can give me a help reference or
sample or even a solution

Thanks,
Bart
 
D

Dan Bass

Bart,

A treeview has many Nodes, and each Node has many Nodes within it.

TreeView1.Nodes.Add("Level1 Node")
TreeView1.Nodes("Level1 Node").Nodes.Add("Level1 Child")

or more correctly

Dim myNode As Windows.Forms.TreeNode = TreeView1.Nodes.Add("Bob")
Dim myNodeChild As Windows.Forms.TreeNode = myNode.Nodes.Add("Bob's
Child")

in the 2nd example, when you add a tree node, it passes a reference of this
new node back, whcih you can store in a variable (myNode in this case).
You can then add nodes to myNode directly because you have a reference to
that node. When you do this, the Add again passes back a reference to the
new node (in this case myNodeChild, which is a 2nd level node in the tree
view).

Hope that helps.
Dan
 
B

Bart Steur

Thanks Dan, I was aware of this way of defining nodes, but my problem is as
follows:

I have a table in a database with the following structure:

ItemID (PK) int auto-ident
ItemName varchar(50)
ItemSequence tinyint
ItemParent int

Where ItemParent is linked/related to ItemID. (ItemParent is an ItemID in
the same table or NULL)

I read this table from the Database ordered by ItemParent, ItemSequence

When the ItemParent Column is NULL the Item is added to the tree at root
level (Where ItemID is the Key of the node and ItemName the text)
When the ItemParent Column is NOT NULL i want the Item to be added to the
Node with the Key equal to the ItemParent. This can be at any level within
the tree.

At the moment I use the Find Method. This works for me, but as I understand
it slows things down. Currently I have only 10 items in the table, but it
can grow up to 5000.

Is there a faster or smoother way to fill the tree. I've read something
about filling the Treeview directly from a table or a view, but I haven't
figured that out yet.

Hope you have an idea.
Thanks in advance,
Bart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top