none
Drag and Drop on Treeview - dragged node is removed? RRS feed

  • Question

  • Hi all,

    I'm having some trouble with dragging and dropping on a treeview control.

    I have a treeview which is populated from data in a table on a worksheet.  What i'd like to do is drag a node and drop it on another node and update the parent information of the dragged node.

    All works fine with the process, that is until the end sub of Private Sub TreeView1_OLEDragDrop is executed.  Once this  executes the node disappears from the tree?

    Code of Private Sub TreeView1_OLEDragDrop below for reference

    'Move the selected node after the destination node is identified
    Private Sub TreeView1_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
      Dim sNode As Node
      Dim dNode As Node
      Dim dNodeKey As String
      Dim dNodeTxt As String
      Dim newNodeKey As String
      Dim newNodeTxt As String
      
      Effect = fmDropEffectMove
     
      Set sNode = TreeView1.SelectedItem
      Set dNode = TreeView1.HitTest(x * 20, y * 20)
      If dNode = sNode Then Exit Sub
      
      sNodeData = Split(sNode, "_")
      dNodeData = Split(dNode, "_")
        
      Set TreeView1.DropHighlight = dNode
        
      'find the 'dropping' node (sNode) in the equipment list
      RowID = Range("Table1[Child]").Cells.Find(What:=sNodeData(1), _
      LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext).Row _
      - ActiveSheet.ListObjects("Table1").HeaderRowRange.Row
            
      'change the 'dropping' (sNode) nodes level number to the 'drop to'(dNode) nodes level number +1
      With Sheet1.ListObjects("Table1")
        Intersect(.DataBodyRange.Rows(RowID).EntireRow, .ListColumns("Level") _
          .DataBodyRange).Value = dNodeData(0) + 1
        
        'change the parent value of sNode to the equipment# of dNode
        Intersect(.DataBodyRange.Rows(RowID).EntireRow, .ListColumns("Parent") _
          .DataBodyRange).Value = dNodeData(1)
      End With
           
        'now try removing the dropping node (sNode)
        UserForm2.TreeView1.Nodes.Remove sNode.Key
        'and adding it back in with its new parent (dNode)
        UserForm2.TreeView1.Nodes.Add dNode.Key, tvwChild, sNode.Key, dNodeData(0) + 1 & "_" & _
            sNodeData(1) & "_" & sNodeData(2) '& "_" & sNodeData(3) & "|"
              
        'Find the 'dropping' (sNode) node in the tree and select it
      For i = 1 To UserForm2.TreeView1.Nodes.Count
        If InStr(LCase(UserForm2.TreeView1.Nodes.Item(i).Text), LCase(sNodeData(1))) > 0 Then
          UserForm2.TreeView1.Nodes.Item(i).Selected = True
          UserForm2.TreeView1.SetFocus
          Exit For 'Exit Sub
        End If
      Next i
    End Sub

    link to onedrive file containing treeview http://1drv.ms/20MRQHK

    Any help will be much appreciated.

    Cheers

    Brad

    Sunday, February 21, 2016 6:07 AM

Answers

  • Drag & Drop implementation can be complicated and without understanding your what you're D&D objectives are it's difficult to point you in the right direction, even how to start will depend if you automatic or manual mode is more appropriate. It would take a while to work out from your file.

    One thing though D&D is normally initiated in the MouseMove event, not mouseDown or nodeClick. Check button is depressed and maybe Shift or ctrl (up to you), if not don't initiate until the mouse has moved a few pixels. Refer to the documentation and other examples out there (though most will be in VB6).

    • Marked as answer by David_JunFeng Thursday, March 3, 2016 6:58 AM
    Tuesday, February 23, 2016 10:09 AM
    Moderator
  • Hi, _Brad_C_

    As far as I know that the Microsoft TreeView control, version 6.0, features built-in drag-and-drop functionality supports drag-and-drop events, such as OLEStartDrag, OLEDragOver, OLECompleteDrag, and OLEDragDrop.

    How to use drag-and-drop functionality in the Microsoft TreeView control, version 6.0, you could click here to refer about How to use TreeView control Drag-and-Drop capabilities in Access 2000

    • Marked as answer by David_JunFeng Thursday, March 3, 2016 6:58 AM
    Wednesday, February 24, 2016 6:16 AM

All replies

  • In a quick look it doesn't look like you've even initiated Drag & Drop with setting  OLEDrag in the MouseMove and allowedeffects in the OLEStartDrag events. It would take a while to unravel the rest of what you're trying to do!

    In passing did the various responses I gave in your other treeview thread (about populating) answer your questions?

    Sunday, February 21, 2016 11:43 AM
    Moderator
  • In passing did the various responses I gave in your other treeview thread (about populating) answer your questions?

    Hi Peter,

    Yes, the code you provided was very helpful.  I'm still determining the level of the node in my data set via a time consuming loop.  This is so I can sort the dataset appropriately before passing it to the treeview populate code.  I've not had too much success getting an unorganised dataset to populate the treeview , but your code for populating the treeview seems very efficient.

    As for my current issue, I'm not 100% sure how to initiate Drag & Drop by setting OLEDrag in the MouseMove event, and allowedeffects in the OLEStartDrag event.  I've done a small amount of research but have failed to uncover anything I found helpful.  At the moment I'm utilising the NodeClick, OLEStartDrag, OLEDragOver, and OLEDragDrop events.  If you could possibly point me in the right direction, I'll see if that fixes the node vanishing issue I currently suffer on dropping.

    Cheers

    Brad


    • Edited by _Brad_C_ Monday, February 22, 2016 12:30 PM
    Monday, February 22, 2016 12:29 PM
  • Drag & Drop implementation can be complicated and without understanding your what you're D&D objectives are it's difficult to point you in the right direction, even how to start will depend if you automatic or manual mode is more appropriate. It would take a while to work out from your file.

    One thing though D&D is normally initiated in the MouseMove event, not mouseDown or nodeClick. Check button is depressed and maybe Shift or ctrl (up to you), if not don't initiate until the mouse has moved a few pixels. Refer to the documentation and other examples out there (though most will be in VB6).

    • Marked as answer by David_JunFeng Thursday, March 3, 2016 6:58 AM
    Tuesday, February 23, 2016 10:09 AM
    Moderator
  • I'd consider my treeview drag and drop objectives to be fairly standard and simple.  I'm quite surprised at the lack of available information on the internet.  Unfortunately I'm a coding novice so all the examples that are available (C#, VB6, VB.Net) have all proved too difficult for me to translate into VBA.

    My objectives are:

    Drag a node, along with its children, and drop it on any other node updating the original data table (on a spreadsheet) and updating the treeview.

    My code does this, however at the very completion of the sub routine for drag and drop, the dragged node is removed from the treeview.  I don't understand why this is, especially when I can step through the code and have every thing look the way it is supposed to, with the only line left to execute being 'end sub'.

    I suspect the reason is my novice / poor coding, but I'm unable to find the issue, and there appears to be a lack of VBA examples that one can learn from.

    Once again, any would be much appreciated.

    Cheers

    Brad

    • Edited by _Brad_C_ Tuesday, February 23, 2016 11:10 PM
    Tuesday, February 23, 2016 11:06 PM
  • Hi, _Brad_C_

    As far as I know that the Microsoft TreeView control, version 6.0, features built-in drag-and-drop functionality supports drag-and-drop events, such as OLEStartDrag, OLEDragOver, OLECompleteDrag, and OLEDragDrop.

    How to use drag-and-drop functionality in the Microsoft TreeView control, version 6.0, you could click here to refer about How to use TreeView control Drag-and-Drop capabilities in Access 2000

    • Marked as answer by David_JunFeng Thursday, March 3, 2016 6:58 AM
    Wednesday, February 24, 2016 6:16 AM