Trying to use Drag & Drop RRS feed

  • Question

  • I am working on a program where I can drag items from a DataGridView control and drop them onto a Treeview control and I can't figure out why I get two nodes dropped instead of one.

    In my program I have a text box where I enter in the name of the Root node. I drop other items onto the root node from the DataGridView control.

    The Root: Touch Display Sub-Assembly is created. The problems start when I drop items from the DataGridView. Everytime I drop something into the Root or parent nodes it creates a node with a title. Example: Camera Chassis and another one that is blank. As shown in the picture above.

    I have been looking on the internet for examples on how to use drag & drop and haven't been very successful at finding code examples that explain the Drag & Drop functionality very well.

    Here is the code I am currently working with:

    Private Sub tvAssembly_DragDrop(sender As Object, _
                                        e As DragEventArgs) _
                                        Handles tvAssembly.DragDrop
            Dim pt As Point
            Dim destinationNode As New TreeNode
            pt = tvAssembly.PointToClient(New Point(e.X, e.Y))
            destinationNode = tvAssembly.GetNodeAt(pt)
            Dim dragedNode As New TreeNode()
            dragedNode.Text = dragedItemText
            'If tvAssembly.SelectedNode.Level = 0 Then
            'This is the Treeview's Root Node
            dragedItemText = ""
            'End If
            'If Not tvAssembly.SelectedNode.IsExpanded Then
            'End If
        End Sub
        Private Sub tvAssembly_DragEnter(sender As Object, _
                                         e As DragEventArgs) _
                                         Handles tvAssembly.DragEnter
            e.Effect = DragDropEffects.Copy
            'With e.Data.GetFormats(True)
            'Select Case True
            '   Case .Contains("System.Windows.Forms.DataGridView")
            'e.Effect = DragDropEffects.Copy
            '    Case .Contains("System.Windows.Forms.TreeView")
            'e.Effect = DragDropEffects.Move
            '    Case Else
            'e.Effect = DragDropEffects.None
            'End Select
            'End With
        End Sub
        Private Sub tvAssembly_DragOver(sender As Object, _
                                        e As DragEventArgs) _
                                        Handles tvAssembly.DragOver
        End Sub
        Private Sub tvAssembly_ItemDrag(sender As Object, _
                                        e As ItemDragEventArgs) _
                                        Handles tvAssembly.ItemDrag
        End Sub
    #End Region
        Private Sub dgvVendorAssm_MouseDown(sender As Object, _
                                            e As MouseEventArgs) _
                                            Handles dgvVendorAssm.MouseDown
            If e.Button = MouseButtons.Left Then
                'I want the description and Part number of the item being dragged
                dragedItemText = _
                    dgvVendorAssm.Rows(dgvVendorAssm.CurrentCell.RowIndex). _
                        Cells(1).Value.ToString() & "[ " & _
                     dgvVendorAssm.Rows(dgvVendorAssm.CurrentCell.RowIndex). _
                        Cells(0).Value.ToString() & "]"
                'Cells(1 & 0) for tree view node
                dgvVendorAssm.DoDragDrop(dragedItemText, DragDropEffects.Copy)
            End If
        End Sub

    Can someone please tell me where I am screwing up?

    Thank in advance,


    Sunday, October 22, 2017 8:07 PM


  • Hi Everyone:

    I solved my own problem.

    On the example I was using it had AddHandler Functions inside the Form_Load event.

     Private Sub frmMain_Load(sender As Object, _
                                 e As EventArgs) _
                                 Handles Me.Load
            AddHandler tvAssembly.DragEnter, AddressOf tvAssembly_DragEnter
            AddHandler tvAssembly.DragDrop, AddressOf tvAssembly_DragDrop
            dgvVendorAssm.AllowDrop = True
            tvAssembly.AllowDrop = True
        End Sub

    I got rid on them and the duplications stopped.



    • Marked as answer by MRM256 Sunday, October 22, 2017 8:23 PM
    Sunday, October 22, 2017 8:23 PM