|
-
Jul 13th, 2005, 03:30 PM
#1
Thread Starter
New Member
[RESOLVED] How do I combine similar items into one item a treeview?
Right now I have the situation in which my treeview looks like:
Plan Heading
. Plan Item 1
. Plan Item 2
Plan Heading
. Plan Item 3
. Plan Item 4
and I would like to use code to combine the two Plan headings into one:
Plan Heading
.Plan Item 1
.Plan Item 2
.Plan Item 3
.Plan Item 4
What code is appropriate?
Thanks!
Mike Fowler
-
Jul 13th, 2005, 08:22 PM
#2
Re: How do I combine similar items into one item a treeview?
I haven't tested this code but it looks like it should work with the operations in this order.
VB Code:
Dim node1 As TreeNode 'The first instance.
Dim node2 As TreeNode 'The duplicate instance.
'Find the duplicate nodes and assign to the above variables here.
Dim nodes As TreeNode() 'The nodes to be moved.
'Remove the nodes from node2 and remove node2.
node2.Nodes.CopyTo(nodes, 0)
node2.Nodes.Clear()
node2.Remove()
'Add the nodes to node1.
node1.Nodes.AddRange(nodes)
-
Jul 14th, 2005, 10:28 AM
#3
Thread Starter
New Member
Re: How do I combine similar items into one item a treeview?
The code you suggested did not completely solve my problem.
I added the following two Treeviews:
TreeView3.Nodes.Add("Plan")
TreeView3.Nodes(0).Nodes.Add("SDP")
TreeView3.Nodes(0).Nodes.Add("BIP")
TreeView4.Nodes.Add("Plan")
TreeView4.Nodes(0).Nodes.Add("SRS")
TreeView4.Nodes(0).Nodes.Add("BBB")
and the code:
Private Sub btnCombine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCombine.Click
Dim myTreeNodeCollection As TreeNodeCollection = TreeView4.Nodes
Dim myTreeNodeArray(TreeView4.Nodes.Count - 1) As TreeNode
Dim tn As TreeNode = TreeView4.SelectedNode
TreeView4.Nodes.CopyTo(myTreeNodeArray, 0)
TreeView4.Nodes.Clear()
TreeView3.Nodes.AddRange(myTreeNodeArray)
TreeView3.Sorted = True
End Sub
And I get the following result in TreeView3:
Plan
.SDP
.BIP
Plan
.SRS
.BBB
and I need the result to be as combined:
Plan
.SDP
.BIP
.SRS
.BBB
Help!
Thanks!
Mike
-
Jul 14th, 2005, 10:41 AM
#4
Re: How do I combine similar items into one item a treeview?
You've left out the "Remove" line. First CopyTo, then Clear, then Remove, then AddRange.
-
Jul 14th, 2005, 11:53 AM
#5
Thread Starter
New Member
Re: How do I combine similar items into one item a treeview?
I left out the Remove line because the Clear statement also removes a treenode.
Having both Clear and Remove is redundant and the program will have a runtime error at the statement:
TreeView4.Nodes.Remove(tn)
An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll
Additional information: Object reference not set to an instance of an object.
this is my code:
Private Sub btnCombine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCombine.Click
Dim myTreeNodeCollection As TreeNodeCollection = TreeView4.Nodes
Dim myTreeNodeArray(TreeView4.Nodes.Count - 1) As TreeNode
Dim tn As TreeNode = TreeView4.SelectedNode
TreeView4.Nodes.CopyTo(myTreeNodeArray, 0)
TreeView4.Nodes.Clear()
TreeView4.Nodes.Remove(tn)
TreeView3.Nodes.AddRange(myTreeNodeArray)
End Sub
-
Jul 14th, 2005, 12:42 PM
#6
Re: How do I combine similar items into one item a treeview?
You are removing all the nodes from TreeView4 and adding them to TreeView3. That is not what you want to do. You want to remove all the child nodes from the root node in TreeView4, then remove the root node from TreeView4, then add the child nodes to the root node of TreeView3.
-
Jul 15th, 2005, 02:21 PM
#7
Thread Starter
New Member
Re: How do I combine similar items into one item a treeview?
I need further instructions...would you please help me with using CopyTo,
Clear, Remove, and AddRange...
every thing I have tried has not worked.
Thanks!
Mike
-
Jul 15th, 2005, 09:54 PM
#8
Re: How do I combine similar items into one item a treeview?
 Originally Posted by jmcilhinney
I haven't tested this code but it looks like it should work with the operations in this order.
VB Code:
Dim node1 As TreeNode 'The first instance.
Dim node2 As TreeNode 'The duplicate instance.
'Find the duplicate nodes and assign to the above variables here.
Dim nodes As TreeNode() 'The nodes to be moved.
'Remove the nodes from node2 and remove node2.
node2.Nodes.CopyTo(nodes, 0)
node2.Nodes.Clear()
node2.Remove()
'Add the nodes to node1.
node1.Nodes.AddRange(nodes)
Isn't that exactly what I've done here. In this example, node2 is the node that you want to be removed and have all its children moved to the other tree, and node1 is the node in the other tree that you want those children to be attached to. In your example, they are the nodes that read "Plan".
-
Jul 19th, 2005, 11:10 AM
#9
Thread Starter
New Member
Re: How do I combine similar items into one item a treeview?
I was successfull finally...
Thank you for your patience!
Here are my initializations:
TreeView3.Nodes.Add("Plan")
TreeView3.Nodes(0).Nodes.Add("SDP")
TreeView3.Nodes(0).Nodes.Add("BIP")
TreeView3.Nodes.Add("Plan")
TreeView3.Nodes(1).Nodes.Add("SRS")
TreeView3.Nodes(1).Nodes.Add("BBB")
Here is my code:
Dim node1 As TreeNode
Dim node2 As TreeNode
TreeView3.Nodes.Add("Plan")
node1 = TreeView3.Nodes(0)
node2 = TreeView3.Nodes(1)
Dim nodes(1) As TreeNode
node2.Nodes.CopyTo(nodes, 0)
node2.Nodes.Clear()
node2.Remove()
node1.Nodes.AddRange(nodes)
'remove the 2 heading if it has no children
Dim iHeadingChildren As Integer
iHeadingChildren = TreeView3.Nodes(1).Nodes.Count
If iHeadingChildren = 0 Then
TreeView3.Nodes(1).Nodes.Remove(TreeView3.Nodes(1))
End If
-
Jul 19th, 2005, 06:04 PM
#10
Re: How do I combine similar items into one item a treeview?
Glad it's working. Don't forget to mark your thread as Resolved from the Thread Tools menu.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|