Programitically Adding items to TreeView

G

Guest

VS,VB 2005

I have a treeview which i load data into in the forms load event
Adding Nodes and Children to the parent nodes works fine

to add a parent:
Me.tvGroups.Nodes.Add(GroupDataRow(0)("MenuGroupID").ToString,
GroupDataRow(0)("GroupLabel"))

to add a child to a Parent:
ParentIndex =
Me.tvGroups.Nodes.IndexOfKey(GroupDataRow(0)("ParentGroupID").ToString)
NodeRef = Me.tvGroups.Nodes(ParentIndex)
NodeRef.Nodes.Add(GroupDataRow(0)("MenuGroupID").ToString,
GroupDataRow(0)("GroupLabel"))

all works good... but now im trying to add a child to a child and i am
unable to get the parent index by key the same way as above. this code fails
when its a child key im looking for:

ParentIndex =
Me.tvGroups.Nodes.IndexOfKey(GroupDataRow(0)("ParentGroupID").ToString)

suggestions???
 
G

Guest

also, if i insert a new node, child, grandchild, etc... how do i make the
inserted node the selected node?
 
G

G Himangi

To select the node, set its Selected property (from .Net 2.0 onwards) to
true.

---------
- G Himangi, Sky Software http://www.ssware.com
Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
Browsing Functionality For Your App (.Net & ActiveX Editions).
EZNamespaceExtensions.Net : Develop namespace extensions rapidly in .Net
EZShellExtensions.Net : Develop all shell extensions,explorer bars and BHOs
rapidly in .Net
 
G

Guest

useing .net 2

first, i can find no Selected Property, what is it under?

second, back to my first problem...

if i am looking for the index to a node that is a granchild, using
IndexOfKey returns a -1, it cant find the granchild. How do i find the Index?

if i have an Index, how do i set the node to selected?
 
G

Guest

solving my first problem gives me the solution for the rest of them

this code is not my origional creation....

i created 2 functions that when passed a Key Value (Nodes.Name) it will
return the Node i referenced and the rest is simple

this allows me to find a node that is a parent, child, grandchild, great
grandchild, etc... by passing the key (or Name) associated with it...

Public Function FindNodeByNodeName(ByVal NodeName As String) As TreeNode
Return SelectNode(Me.tvGroups.Nodes, NodeName)
End Function

Private Function SelectNode(ByVal Nodes As TreeNodeCollection, ByVal
NodeName As String) As TreeNode
Dim foundNode As TreeNode = Nothing
Dim i As Integer = 0

Do Until Not foundNode Is Nothing OrElse i = Nodes.Count
If DirectCast(Nodes(i), TreeNode).Name = NodeName Then
foundNode = DirectCast(Nodes(i), TreeNode)
Else
If Nodes(i).Nodes.Count > 0 Then
foundNode = SelectNode(Nodes(i).Nodes, NodeName)
End If

i += 1
End If
Loop
Return foundNode
End Function
 
G

Guest

..Selected???

this works...

Me.tvName.SelectedNode = Me.tvName.Nodes(NodeIndexToSelect)
 
G

Guest

while were at it, to locate a child, grandchild, etc.. to add another node
to...

could be spead up by useing tvname.nodes.find(KeyText,True) to see if you
should even bother going down the tree any further
 

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