The Treeview Drag&Drop AND using custom TreeNode !

Here is the code that I use to  carry out a drag & drop on my TreeView, it goes very well ! 
but when I use my own custom TreeNode (named : 'UsrTreeNode' and have some properties added) instead of the original 'System.Windows.Forms.TreeNode' , the drag & drop do not have any effect.
I tried doing something like :

1) My own custom TreeNode.

<Code>
Public Class UsrTreeNode

    Inherits System.Windows.Forms.TreeNode
    Private NodeType As String

   Public Sub New(ByVal [text] As [String], ByVal [Id] As [Double], ByVal [ImgIndx] As Integer, ByVal [SelImgIndx] As Integer)

        MyBase.New([text],[ImgIndx], [SelImgIndx])
        MyBase.Tag = [Id]
    End Sub

    Public Property Type()
        Get
            Return NodeType
        End Get
        Set(ByVal Value)
            NodeType = Value
        End Set
    End Property
End Class
</Code>
 
2) My DragDrop event :

If I populate my TreeView with my custom UsrTreeNode instead of original TreeNode and use the same code (by replacing Treenode by UsrTreeNode)  my drag and drop doesn't work and no error raised !! 

<Code>

Public Sub TreeView_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles Trv.DragDrop

        Dim NewNode As UsrTreeNode

      '----If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", False) Then

       If e.Data.GetDataPresent("MyApp.UsrTreeNode", False) Then
            Dim pt As Point
            Dim DestinationNode As UsrTreeNode
            pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))

            DestinationNode = CType(sender, TreeView).GetNodeAt(pt)

            'NewNode = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)

            NewNode = CType(e.Data.GetData("MyApp.UsrTreeNode"), UsrTreeNode)

            'DestinationNode.Nodes.Add(NewNode.Clone)

            DestinationNode.Expand()

            'Remove original node
            NewNode.Remove()
        End If
    End Sub

</Code>

Any idea.

Thanks 


Answer this question

The Treeview Drag&Drop AND using custom TreeNode !

  • venkateshwar rao gundlapally

    You have reson Jacob, in fact, the step by step Debug revealed the culprit of the twice copy ! it's my Sub BeforeExpand_expand on which I point after the : DestinationNode.Expand().  Thank you Jacob for your assistance, it works very well now. I'a very satisfied.
  • Bryan St. Amour

    That works fine by using the Overrides Function Clone, but if I drag&drop a node, I have all its children nodes in double (copied twice) !!

    (I use : DestinationNode.Nodes.Add(NewNode.Clone)  to add my NewNode CLONE to my TreeView)


  • ngc

    That's really strange...  It looks like you have code running through the child nodes twice.  Are you, perchance, looping through all the children of the nodes and calling Clone on them rather than just cloning the parentNode   Based on the code you have posted, it certainly doesn't look like it . . . The clone method I posted is something I have used in a number of projects and I have never had a problem like this . . . 
  • Trevor Hancock MSFT

    I think that the recursive part of the method must be, it allows to have all children cloned (with all new propoerties as I want).
    but here is what happens :

    before grag&drop :

    ParentNode
          |_______ Child_1
          |_______ Child_2
          |_______ Child_3

    After Drag&Drop to a DestinationNode  i obtain :
         
    DestinationNode
    ......|
    ......|__ Parent node
    .................|_______ Child_1
    .................|_______ Child_2
    .................|_______ Child_3
    .................|_______ Child_1
    .................|_______ Child_2
    .................|_______ Child_3

    Instead of :

    DestinationNode
    ......|
    ......|__ Parent node
    .................|_______ Child_1
    .................|_______ Child_2
    .................|_______ Child_3


    how I can remove doubled nodes  

  • Oyster Poh

    Excellent... Glad it worked out.
  • FrdBed

    What happens when you add the second parameter to the GetData method   This works for me in my world of custom treenode drag-drop:

    NewNode = CType(e.Data.GetData("MyApp.UsrTreeNode", True), UsrTreeNode)


    Of course, it's also useful to make sure that your Type string is fully qualified and you don't have a phantom namespace somewhere.

  • porov

    Not sure if your code above is an exact copy of what you are using, but the code that you have to add the NewNode to the DestinationNode is commented out . . . 
  • Mark Phillips

    The clone method that I posted clones all children nodes as well.  So, when you add the cloned parent node, it will have all of it's children as well...  If you don't want that, then you can take out the recursive part of the method:

    Public Overrides Function Clone() As Object
        Dim CloneNode As YourNode = New YourNode()

    'copying my properties
        CloneNode.Prop1 = Prop1
        CloneNode.Prop2 = Prop2

        Return CloneNode
    End Function

  • Andr&amp;#233; Alves

    After deleting of the sub New (...) in my UsrTreeNode Class, I can perform a drag&Drop now on my TreeView. So, The GetData retreive my NewNode without problem, but at : NewNode.clone I loose the TreeNode Type property witch I have defined for my custom TreeNode !
    Here is my UsrTreeNode Class :
    <Code>
    Public Class UsrTreeNode
    Inherits System.Windows.Forms.TreeNode

        Private NodeType As String
        Public Property Type()
            Get
                Return NodeType
            End Get
            Set(ByVal Value)
                NodeType = Value
            End Set
        End Property
    End Class
    </Code>

    and here is my Drag&Drop sub :
    <Code>
    Public Sub TreeView_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles Trv.DragDrop

            Dim NewNode As UsrTreeNode

            If e.Data.GetDataPresent("GPI.UsrTreeNode", False) Then
                Dim pt As Point
                Dim DestinationNode As UsrTreeNode
                pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))

                DestinationNode = CType(sender, TreeView).GetNodeAt(pt)

                 NewNode = CType(e.Data.GetData("GPI.UsrTreeNode",True), UsrTreeNode)

                DestinationNode.Nodes.Add(NewNode.Clone)

                DestinationNode.Expand()

                'Remove original node
                NewNode.Remove()
            End If
        End Sub
    </Code>

    Any Idea please 

  • mjt

    Sorry,  I forgot to omit the comment on : [DestinationNode.Nodes.Add(NewNode.Clone)]

    the code that I use is as follows:
    <Code>

    Public Sub TreeView_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles Trv.DragDrop
            Dim NewNode As UsrTreeNode

           If e.Data.GetDataPresent("MyApp.UsrTreeNode", False) Then
                Dim pt As Point
                Dim DestinationNode As UsrTreeNode
                pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
                DestinationNode = CType(sender, TreeView).GetNodeAt(pt)

               NewNode = CType(e.Data.GetData("MyApp.UsrTreeNode"), UsrTreeNode)

               DestinationNode.Nodes.Add(NewNode.Clone)
               
               DestinationNode.Expand()

                'Remove original node
                NewNode.Remove()
            End If
        End Sub
    </Code>

    Thanks.

  • SMILEMan

    Treenode Clone isn't flawless, so you will need to provide your own implementation when using a Custom Treenode as it doesn't automatically copy everything.  Something like this works:

    Public Overrides Function Clone() As Object
        Dim CloneNode As YourNode = New YourNode()
        Dim TempNode As YourNode
        If Nodes.Count > 0 Then
         For Each TempNode In Nodes
             CloneNode.Nodes.Add(CType(TempNode.Clone, YourNode))
         Next
        End If

    'copying my properties
        CloneNode.Prop1 = Prop1
        CloneNode.Prop2 = Prop2

        Return CloneNode
    End Function

  • The Treeview Drag&Drop AND using custom TreeNode !