Does anyone have a custom function to Remove a child node from a TreeView and move it to another child?

That may not make sense, so here's an Example:

Private Sub Form_Load()
     Dim Test As Node
     Dim Test2 As Node

     Set Test = TreeView1.Nodes.Add(, , "Testing", "Testing")
     Test.Expanded = True
     Test.Bold = True

     Set Test2 = TreeView1.Nodes.Add(, , "Testing2, "Testing2")
     Test2.Expanded = True
     Test2.Bold = True

     TreeView1.Nodes.Add "Testing", tvwChild, "hello"

'function to Remove child node from parent and add it to another part.. ?? :lol:
Private Sub RemoveChildNode(tvTree As TreeView, strName As String)
     Dim i As Integer
     For i = tvTree.Nodes.Count To 0 Step - 1
          If strName = tvTree.Nodes("Testing").Text Then 'or something...
               ' remove strName from "Testing" part
               ' and add strName to "Testing2" part
          End If
     Next i
End Sub


'usage:
RemoveChildNode(TreeView1,"hello")

Get my drift? :D

Thanks in advance.

Dani AI

Generated

The VB6 TreeView does not let you change a node's parent in-place. You have to recreate the node under the new parent and then remove the original. 's setup is fine and 's Remove/Add approach will work for a simple leaf, but it loses Tag, Image/SelectedImage, Bold/Expanded state and any child nodes. The helper below moves a single node (found by text), preserves common properties, and avoids duplicate-key errors. For full subtree moves you can extend this into a recursive clone before removing the original.

Private Sub MoveNodeByText(tv As TreeView, srcText As String, destParentKey As String)
    Dim i As Long
    Dim src As Node
    Dim dest As Node
    Dim newNode As Node
    Dim newKey As String

    ' find source node by text (first match)
    For i = 1 To tv.Nodes.Count
        If StrComp(Trim(tv.Nodes(i).Text), Trim(srcText), vbTextCompare) = 0 Then
            Set src = tv.Nodes(i)
            Exit For
        End If
    Next i
    If src Is Nothing Then Exit Sub

    ' locate destination parent by key
    On Error Resume Next
    Set dest = tv.Nodes(destParentKey)
    On Error GoTo 0
    If dest Is Nothing Then Exit Sub

    ' create a simple unique key (avoids duplicate-key error while source still exists)
    newKey = destParentKey & "_m" & CStr(Timer * 1000) & "_" & CStr(tv.Nodes.Count)

    ' add new node under destination and copy common properties
    Set newNode = tv.Nodes.Add(dest.Key, tvwChild, newKey, src.Text)
    If Not newNode Is Nothing Then
        newNode.Tag = src.Tag
        newNode.Image = src.Image
        newNode.SelectedImage = src.SelectedImage
        newNode.Bold = src.Bold
        newNode.Expanded = src.Expanded
    End If

    ' remove original node (children are removed too)
    tv.Nodes.Remove src.Index
End Sub

Notes: prefer using Keys to locate nodes where possible. If you need to preserve a subtree and keep original keys, first walk the subtree into an in-memory structure (text, key, tag, image, children), remove the original subtree, then recreate it under the target parent (a recursive clone). Also avoid modifying the Nodes collection while iterating it; collect references first, then perform Add/Remove.

Hi,

Try this :

Private Sub RemoveChildNode(tvTree As TreeView, strName As String)
Dim i As Integer
Dim NFound As Boolean
NFound = False
For i = tvTree.Nodes.Count To 0 Step - 1
  If Trim(strName) = Trim(tvTree.Nodes(i).Text)  Then          
    TvTree.Nodes.Remove TvTree.Nodes(i)
    NFound = True
    Exit For
  End If
Next i
If NFound Then
   TVTree.Nodes.Add "Testing2", tvwChild, strName, strName
End If
End Sub

Regards
Veena

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.