Parent and Child for a Treeview.

G

Guest

the code below dows not let me get the parent child view... all the nodes are
show in one line only...

also i need them to be collasped ...

Thanks ..




Private Sub GetTree()
Dim objNode As TreeNode
Dim i, j As Integer

TreeView1.ImageList = ImageList1

'Create the Server Node
objNode = New TreeNode
objNode.Text = TextBox1.Text
TreeView1.Nodes.Add(objNode)

For Each objProject In objSession.Projects

objNode = TreeView1.Nodes.Add(objProject.Name)

'Loop thru Baselines
For Each objBaseline In objProject.Baselines
If objBaseline.Name <> "Deleted View" Then

objNode = TreeView1.Nodes.Add(objBaseline.Name)

If objBaseline.Name = "Default View" Then
objNode.Text = "Current Baseline"
Else
objNode.Text = objBaseline.Name
End If
For Each objReqType In objBaseline.RequirementTypes
Application.DoEvents() ' Allow for a break, so user
can cancel

objNode = TreeView1.Nodes.Add(objReqType.Name)

Next objReqType
End If
Next objBaseline
Next objProject
objNode = Nothing
Exit Sub

End Sub
 
R

Robin Tucker

Yes, you have effectively built a flat tree. At each nesting level, you
need to add the nodes further down the branches to their parent - so, simply
store the parent in "root", "firstlevel", "secondlevel" etc.....


Try this....




Private Sub GetTree()
Dim root, firstLevel, SecondLevel, thirdLevel As TreeNode
Dim i, j As Integer

TreeView1.ImageList = ImageList1

'Create the Server Node
root= New TreeNode
root.Text = TextBox1.Text
TreeView1.Nodes.Add(root)

For Each objProject In objSession.Projects

firstLevel = root.Nodes.Add(objProject.Name)

'Loop thru Baselines
For Each objBaseline In objProject.Baselines
If objBaseline.Name <> "Deleted View" Then

secondLevel= firstLevel.Nodes.Add(objBaseline.Name)

If objBaseline.Name = "Default View" Then
secondLevel.Text = "Current Baseline"
Else
secondLevel.Text = objBaseline.Name
End If
For Each objReqType In objBaseline.RequirementTypes
Application.DoEvents() ' Allow for a break, so user
can cancel

thirdLevel = secondLevel.Nodes.Add(objReqType.Name)

Next objReqType
End If
Next objBaseline
Next objProject

Exit Sub

End Sub
 
G

Guest

Thank you soo much that helped.

Iam trying to add image to each level.

i have it as:

root.imageindex = 0
firstlevel.imageindex = 1 and so on..

i add this below the
root= New TreeNode
root.Text = TextBox1.Text
TreeView1.Nodes.Add(root)

initally it show all the images right.. but when i click any child or parent
it changes to image to the image of the root.

why wud that happen

thank you
 
R

Robin Tucker

Well, there are two image indices - one is ImageIndex and the other I think
is SelectedIndex (or something) - you can effectively use one icon for when
it's selected and another for when it isn't. For now, set
selectedimageindex to the same as you set imageindex and all will be well.
 
G

Guest

Thanks for ur help..

if i use Treeview.Afterselect does not let me go to the lowest level to
select a node... I want to travel to the last node or the deepest node at
thirdlevel and then check the type of the node selected and go from there...

if i use afterselect it immediately goes to the fucntion as soon as clik any
node in the tree..

thank you
 
R

Robin Tucker

If you want to go to the lowest level to select a node, then you need some
algorithms :) ; recursive tree traversals to find the "leaves".

When you say "you want to travel", do you mean "you" the program, or "you"
the user. If the user selects a node, you don't have a problem. If you
want to programatically choose a node or group of nodes, you need to be more
specific. Please enlighten me as to which node or nodes you want to check
and why you want to check them. We can then figure an algorithm to do this.
 
G

Guest

Hi,
Thank you for ur help.

The user needs to select the last child node in a tree. for ex:
child1---
Child2----
Child3.

so the user need to select child3 and then click a button to perform some
action.

Hope iam being clear.

Thank you.
 
R

Robin Tucker

Without getting too complicated (ie. I would derive new classes from
TreeViewItem so you can identify what "type" of object the tree node is,
regardless of it's "level"), you can find the depth of the item in the tree
by doing something like this:

Dim theDepth as integer =
TreeView.SelectedNode.FullPath.Split("\"c).Length()

If your tree structure is fixed with a depth of 4 (including the root), then
you will know what type of item the user is working with when he clicks the
button, or you can do this in "after select" to switch available buttons on
or off depending on which type of node the user has selected:

Select Case theDepth

case 1

' The root item

case 2

' an objProject

case 3

' an objBaseline

case 4

' an objReqType

End Select


If you then want to modify the properties of the item representing this in
the tree, well, things get a bit more complicated. What I do is ALWAYS
create a hashtable with a tree view to hash some key value to the actual
tree view item.

So,

Private m_Hash as New HashTable


and then when you build your tree........




Private Sub GetTree()

Dim root, firstLevel, SecondLevel, thirdLevel As TreeNode
Dim i, j As Integer

TreeView1.ImageList = ImageList1

root = New TreeNode
root.Text = TextBox1.Text
TreeView1.Nodes.Add(root)

m_Hash.Add ( root.FullPath, root )

For Each objProject In objSession.Projects

firstLevel = root.Nodes.Add(objProject.Name)

For Each objBaseline In objProject.Baselines
If objBaseline.Name <> "Deleted View" Then

secondLevel= firstLevel.Nodes.Add(objBaseline.Name)

If objBaseline.Name = "Default View" Then
secondLevel.Text = "Current Baseline"
Else
secondLevel.Text = objBaseline.Name
End If

m_Hash.Add ( secondLevel.FullPath, secondLevel )

For Each objReqType In objBaseline.RequirementTypes
Application.DoEvents() '
thirdLevel = secondLevel.Nodes.Add(objReqType.Name)


m_Hash.Add ( thirdLevel.FullPath, thirdLevel )

Next objReqType

End If

Next objBaseline
Next objProject

End Sub




now, using the above code, fetching the object represented by your tree view
item is simply a case of doing this:


Dim theObject as Object = m_Hash ( TreeView1.SelectedNode.FullPath )


' Cast the object to whatever, according to what type it is:


If TypeOf ( theObject ) Is objProjectClass Then

Dim theObjProject as objProjectClass = DirectCast(theObject,
theObjProjectClass )

ElseIf TypeOf ( theObject ) Is objBaselineClass Then

Dim theObjBaseline as objBaselineClass = DirectCast(theObject,
objBaselineClass )

ElseIf TypeOf ( theObject ) Is objReqTypeClass Then

Dim theObjReqType as objReqTypeClass = DirectCast(theObject,
objReqTypeClass )

End If







I would like to suggest you switch ON option STRICT and option Explicit.
You will find it catches a lot of annoying bugs ;)
 

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