Results 1 to 8 of 8

Thread: Change the parent-child relation in a treeview control at runtime

  1. #1
    New Member
    Join Date
    Feb 10
    Posts
    14

    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

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,763

    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.

  3. #3
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,237

    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim n1 As TreeNode
    3.         Dim n3 As TreeNode
    4.         n1 = GetNodeByKey("Node1")
    5.         n3 = GetNodeByKey("Node3")
    6.         n1.Remove()
    7.         n3.Nodes.Add(n1)
    8.     End Sub
    9.  
    10.     Private Function GetNodeByKey(ByVal strKey As String) As TreeNode
    11.  
    12.         Dim aFindResult As TreeNode() = TreeView1.Nodes.Find(strKey, True)
    13.         Dim tvnRet As TreeNode = Nothing
    14.  
    15.         For j As Integer = 0 To aFindResult.Length - 1
    16.             If aFindResult(j).Name = strKey Then
    17.                 tvnRet = aFindResult(j)
    18.                 Exit For
    19.             End If
    20.         Next
    21.  
    22.         Return tvnRet
    23.  
    24.     End Function

    You must take care to not move node to one of its children

  4. #4
    New Member
    Join Date
    Feb 10
    Posts
    14

    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:
    1. Public Class Form1
    2.     Dim tNode(6) As TreeNode
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         Call initialTree1(TreeView1)
    6.  
    7.     End Sub
    8.  
    9.  
    10.     Private Sub initialTree(ByVal tv As TreeView)
    11.         tv.Nodes.Clear()
    12.         Call totalNodes()
    13.         tv.Nodes.Add(tNode(0))
    14.         tv.Nodes(0).Nodes.Add(tNode(1))
    15.         tv.Nodes(0).Nodes.Add(tNode(2))
    16.         tv.Nodes(0).Nodes.Add(tNode(3))
    17.         tv.Nodes(0).Nodes(1).Nodes.Add(tNode(4))
    18.         tv.Nodes(0).Nodes(1).Nodes.Add(tNode(5))
    19.         tv.Nodes(0).Nodes(0).Nodes.Add(tNode(6))
    20.     End Sub
    21.  
    22.  
    23.     Private Sub initialTree1(ByVal tv As TreeView)
    24.         tv.Nodes.Clear()
    25.         Call totalNodes()
    26.         tv.Nodes.Add(tNode(1))
    27.         tv.Nodes(0).Nodes.Add(tNode(0))
    28.         tv.Nodes(0).Nodes.Add(tNode(6))
    29.         tv.Nodes(0).Nodes(0).Nodes.Add(tNode(3))
    30.         tv.Nodes(0).Nodes(0).Nodes.Add(tNode(2))
    31.         tv.Nodes(0).Nodes(0).Nodes(1).Nodes.Add(tNode(4))
    32.         tv.Nodes(0).Nodes(0).Nodes(1).Nodes.Add(tNode(5))
    33.     End Sub
    34.     Private Sub initialTree2(ByVal tv As TreeView)
    35.         tv.Nodes.Clear()
    36.         Call totalNodes()
    37.         tv.Nodes.Add(tNode(2))
    38.         tv.Nodes(0).Nodes.Add(tNode(0))
    39.         tv.Nodes(0).Nodes.Add(tNode(4))
    40.         tv.Nodes(0).Nodes.Add(tNode(5))
    41.  
    42.         tv.Nodes(0).Nodes(0).Nodes.Add(tNode(1))
    43.         tv.Nodes(0).Nodes(0).Nodes.Add(tNode(3))
    44.         tv.Nodes(0).Nodes(0).Nodes(0).Nodes.Add(tNode(6))
    45.     End Sub
    46.  
    47.  
    48.     Private Sub totalNodes()
    49.         Dim i As Integer
    50.  
    51.         For i = 0 To 6
    52.             tNode(i) = New TreeNode
    53.             tNode(i).Name = "Node" + i.ToString
    54.             tNode(i).Text = "Node" + i.ToString
    55.         Next
    56.     End Sub
    57.  
    58.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    59.        
    60.         Call initialTree(TreeView1)
    61.  
    62.     End Sub
    63.  
    64.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    65.         Call initialTree2(TreeView1)
    66.     End Sub
    67. End Class

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,763

    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.

  6. #6
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,237

    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?

  7. #7
    New Member
    Join Date
    Feb 10
    Posts
    14

    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.

  8. #8
    Frenzied Member
    Join Date
    Sep 06
    Posts
    1,237

    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:
    1. Public Class Form1
    2.     Dim tNode(6) As TreeNode
    3.  
    4.  
    5.     Private Sub initialTree(ByVal tv As TreeView)
    6.         tv.Nodes.Clear()
    7.         Call totalNodes()
    8.         tv.Nodes.Add(tNode(0))
    9.         tv.Nodes(0).Nodes.Add(tNode(1))
    10.         tv.Nodes(0).Nodes.Add(tNode(2))
    11.         tv.Nodes(0).Nodes.Add(tNode(3))
    12.         tv.Nodes(0).Nodes(1).Nodes.Add(tNode(4))
    13.         tv.Nodes(0).Nodes(1).Nodes.Add(tNode(5))
    14.         tv.Nodes(0).Nodes(0).Nodes.Add(tNode(6))
    15.     End Sub
    16.  
    17.     Private Sub totalNodes()
    18.         Dim i As Integer
    19.  
    20.         For i = 0 To 6
    21.             tNode(i) = New TreeNode
    22.             tNode(i).Name = "Node" + i.ToString
    23.             tNode(i).Text = "Node" + i.ToString
    24.         Next
    25.     End Sub
    26.  
    27.  
    28.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    29.         Call initialTree(TreeView1)
    30.     End Sub
    31.  
    32.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    33.         ' State 1
    34.         SetHighPressureNode(TreeView1, TreeView2, GetNodeByKey("Node1", TreeView1), GetNodeByKey("Node0", TreeView1))
    35.  
    36.         'State 2
    37.         'SetHighPressureNode(TreeView1, TreeView2, GetNodeByKey("Node2", TreeView1), GetNodeByKey("Node0", TreeView1))
    38.     End Sub
    39.  
    40.     Private Sub SetHighPressureNode(ByVal tvOriginalTree As TreeView, ByVal tvWorkingTree As TreeView, ByVal tvnHighPressureNode As TreeNode, ByVal tvnLowPressureNode As TreeNode)
    41.         Dim tvnHighClone As TreeNode
    42.         Dim tvnLowClone As TreeNode
    43.         Dim tvnNewHigh As TreeNode
    44.         Dim strOldName As String
    45.  
    46.         tvWorkingTree.Nodes.Clear() ' Clear working tree nodes
    47.  
    48.         tvnHighClone = tvnHighPressureNode.Clone ' Copy high pressure node
    49.         tvnLowClone = tvnLowPressureNode.Clone ' Copy low pressure node
    50.  
    51.         tvWorkingTree.Nodes.Add(tvnHighClone) ' Add copied high pressure node to the working tree root
    52.         tvnNewHigh = GetNodeByKey(tvnHighClone.Name, tvWorkingTree)
    53.         strOldName = tvnHighClone.Name ' Store name
    54.         tvnNewHigh.Name = "blabla-blabla" ' Chenge name in order to not find it
    55.         tvnNewHigh.Nodes.Insert(0, tvnLowClone)
    56.  
    57.         GetNodeByKey(strOldName, tvWorkingTree).Remove() ' Remove tvnHighPressureNode from its original place.
    58.         tvnNewHigh.Name = strOldName 'Restore original name.
    59.  
    60.     End Sub
    61.  
    62.     Private Function GetNodeByKey(ByVal strKey As String, ByVal tvTree As TreeView) As TreeNode
    63.  
    64.         Dim aFindResult As TreeNode() = tvTree.Nodes.Find(strKey, True)
    65.         Dim tvnRet As TreeNode = Nothing
    66.  
    67.         For j As Integer = 0 To aFindResult.Length - 1
    68.             If aFindResult(j).Name = strKey Then
    69.                 tvnRet = aFindResult(j)
    70.                 Exit For
    71.             End If
    72.         Next
    73.  
    74.         Return tvnRet
    75.  
    76.     End Function
    77. 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
  •