|
-
May 5th, 2012, 09:08 AM
#1
Thread Starter
New Member
Change the parent-child relation in a treeview control at runtime
I want to program a water supply pipeline network in a treeview control. It has five node (junctions). The parent-child relation is based on a parameter (say pressure).
Initially, the node 1 is parent and its child nodes are 2 and 3. The node 3 has child nodes 4 and 5.
At run time the pressure changes. Now, node 3 is parent and its child nodes are 1, 4 and 5. The node 1 has child node 2.
It could be easier to show in a figure, but I donot know the procedure to attach or insert a figure. sorry.
Please advise me to handle such changes in the paren-child nodes relation in the treeview control.
Thanks
-
May 5th, 2012, 10:43 AM
#2
Re: Change the parent-child relation in a treeview control at runtime
Each TreeNode has a Nodes property which is a collection containing its child nodes. They are just collections like any other, so you can Add and Remove items like you can any other collection. The TreeView also has a Nodes collection, which contains the top-level nodes.
-
May 5th, 2012, 11:05 AM
#3
Re: Change the parent-child relation in a treeview control at runtime
You must give each node a unique name (key) e.g. "Node1","Node2", etc..., to move node from one place to another, use code like this
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n1 As TreeNode Dim n3 As TreeNode n1 = GetNodeByKey("Node1") n3 = GetNodeByKey("Node3") n1.Remove() n3.Nodes.Add(n1) End Sub Private Function GetNodeByKey(ByVal strKey As String) As TreeNode Dim aFindResult As TreeNode() = TreeView1.Nodes.Find(strKey, True) Dim tvnRet As TreeNode = Nothing For j As Integer = 0 To aFindResult.Length - 1 If aFindResult(j).Name = strKey Then tvnRet = aFindResult(j) Exit For End If Next Return tvnRet End Function
You must take care to not move node to one of its children
-
May 5th, 2012, 01:45 PM
#4
Thread Starter
New Member
Re: Change the parent-child relation in a treeview control at runtime
Thanks for your help. Actually, I could not explained it properly in the text. It could be easy with a figure to explain. Now, I tried to create the node structure for three cases.
1. Initially, the pressure at node 0 is high and the node structure is as programmed in InitialTree.
2. the pressure at node 1 is high and the node structure is as programmed in InitialTree1. It is obtained on clicking button1.
3. the pressure at node 2 is high and the node structure is as programmed in InitialTree2. It is obtained on clicking button2.
It can be observed that the initial connection structure of nodes is mentained. I want to do it in the program depending on the pressure of any node in the original treeview control.
Any help is appreciated.
Thanks
vb Code:
Public Class Form1
Dim tNode(6) As TreeNode
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call initialTree1(TreeView1)
End Sub
Private Sub initialTree(ByVal tv As TreeView)
tv.Nodes.Clear()
Call totalNodes()
tv.Nodes.Add(tNode(0))
tv.Nodes(0).Nodes.Add(tNode(1))
tv.Nodes(0).Nodes.Add(tNode(2))
tv.Nodes(0).Nodes.Add(tNode(3))
tv.Nodes(0).Nodes(1).Nodes.Add(tNode(4))
tv.Nodes(0).Nodes(1).Nodes.Add(tNode(5))
tv.Nodes(0).Nodes(0).Nodes.Add(tNode(6))
End Sub
Private Sub initialTree1(ByVal tv As TreeView)
tv.Nodes.Clear()
Call totalNodes()
tv.Nodes.Add(tNode(1))
tv.Nodes(0).Nodes.Add(tNode(0))
tv.Nodes(0).Nodes.Add(tNode(6))
tv.Nodes(0).Nodes(0).Nodes.Add(tNode(3))
tv.Nodes(0).Nodes(0).Nodes.Add(tNode(2))
tv.Nodes(0).Nodes(0).Nodes(1).Nodes.Add(tNode(4))
tv.Nodes(0).Nodes(0).Nodes(1).Nodes.Add(tNode(5))
End Sub
Private Sub initialTree2(ByVal tv As TreeView)
tv.Nodes.Clear()
Call totalNodes()
tv.Nodes.Add(tNode(2))
tv.Nodes(0).Nodes.Add(tNode(0))
tv.Nodes(0).Nodes.Add(tNode(4))
tv.Nodes(0).Nodes.Add(tNode(5))
tv.Nodes(0).Nodes(0).Nodes.Add(tNode(1))
tv.Nodes(0).Nodes(0).Nodes.Add(tNode(3))
tv.Nodes(0).Nodes(0).Nodes(0).Nodes.Add(tNode(6))
End Sub
Private Sub totalNodes()
Dim i As Integer
For i = 0 To 6
tNode(i) = New TreeNode
tNode(i).Name = "Node" + i.ToString
tNode(i).Text = "Node" + i.ToString
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call initialTree(TreeView1)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call initialTree2(TreeView1)
End Sub
End Class
-
May 5th, 2012, 08:10 PM
#5
Re: Change the parent-child relation in a treeview control at runtime
As I have already said, to move a node you simply Remove it from the Nodes collection of its current parent and Add it to the Nodes collection of its new parent.
-
May 5th, 2012, 08:31 PM
#6
Re: Change the parent-child relation in a treeview control at runtime
Are you going to work with these nodes only?
If Node3, 4, 5 or 6 gets high pressure, how the structure will look?
-
May 5th, 2012, 11:19 PM
#7
Thread Starter
New Member
Re: Change the parent-child relation in a treeview control at runtime
Thanks. It is just an example. there may be more nodes and any node can have high pressure. But the initial structure of nodes will remian same.
I do appreciate your help.
-
May 9th, 2012, 02:58 PM
#8
Re: Change the parent-child relation in a treeview control at runtime
Sorry for delay!
Try this code
Add two TreeView controls, TreeView1 which will be the original structure, TreeView2 which will show the new structure.
vb Code:
Public Class Form1 Dim tNode(6) As TreeNode Private Sub initialTree(ByVal tv As TreeView) tv.Nodes.Clear() Call totalNodes() tv.Nodes.Add(tNode(0)) tv.Nodes(0).Nodes.Add(tNode(1)) tv.Nodes(0).Nodes.Add(tNode(2)) tv.Nodes(0).Nodes.Add(tNode(3)) tv.Nodes(0).Nodes(1).Nodes.Add(tNode(4)) tv.Nodes(0).Nodes(1).Nodes.Add(tNode(5)) tv.Nodes(0).Nodes(0).Nodes.Add(tNode(6)) End Sub Private Sub totalNodes() Dim i As Integer For i = 0 To 6 tNode(i) = New TreeNode tNode(i).Name = "Node" + i.ToString tNode(i).Text = "Node" + i.ToString Next End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Call initialTree(TreeView1) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' State 1 SetHighPressureNode(TreeView1, TreeView2, GetNodeByKey("Node1", TreeView1), GetNodeByKey("Node0", TreeView1)) 'State 2 'SetHighPressureNode(TreeView1, TreeView2, GetNodeByKey("Node2", TreeView1), GetNodeByKey("Node0", TreeView1)) End Sub Private Sub SetHighPressureNode(ByVal tvOriginalTree As TreeView, ByVal tvWorkingTree As TreeView, ByVal tvnHighPressureNode As TreeNode, ByVal tvnLowPressureNode As TreeNode) Dim tvnHighClone As TreeNode Dim tvnLowClone As TreeNode Dim tvnNewHigh As TreeNode Dim strOldName As String tvWorkingTree.Nodes.Clear() ' Clear working tree nodes tvnHighClone = tvnHighPressureNode.Clone ' Copy high pressure node tvnLowClone = tvnLowPressureNode.Clone ' Copy low pressure node tvWorkingTree.Nodes.Add(tvnHighClone) ' Add copied high pressure node to the working tree root tvnNewHigh = GetNodeByKey(tvnHighClone.Name, tvWorkingTree) strOldName = tvnHighClone.Name ' Store name tvnNewHigh.Name = "blabla-blabla" ' Chenge name in order to not find it tvnNewHigh.Nodes.Insert(0, tvnLowClone) GetNodeByKey(strOldName, tvWorkingTree).Remove() ' Remove tvnHighPressureNode from its original place. tvnNewHigh.Name = strOldName 'Restore original name. End Sub Private Function GetNodeByKey(ByVal strKey As String, ByVal tvTree As TreeView) As TreeNode Dim aFindResult As TreeNode() = tvTree.Nodes.Find(strKey, True) Dim tvnRet As TreeNode = Nothing For j As Integer = 0 To aFindResult.Length - 1 If aFindResult(j).Name = strKey Then tvnRet = aFindResult(j) Exit For End If Next Return tvnRet End Function End Class
It works fine for the nodes example you provide, i hope it works fine for any nodes count and any nodes structure.
Last edited by 4x2y; May 9th, 2012 at 03:02 PM.
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
|