Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Treeview DragDrop...

Posted on 1999-06-28
4
Medium Priority
?
629 Views
Last Modified: 2012-08-14
Is it possible to drag and drop items from one treeview control to another treeview control?
0
Comment
Question by:maclean070997
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 300 total points
ID: 1997816
Here's a little sample with Access.  Access doesn't give you the normal drag/drop functionality exposed by VB, but you can still use the TreeView's OLE DragDrop Functions to do DragDrops between trees-- kind of overkill, but it works.

Add two treeviews to a form-- call one TreeView1 and the other TreeView2.  Then add the following code to the form's module.  One note, the TreeView control requires a full click (not just a mousedown event) to cause a node selection.  So you'll have to make sure that you actually select the node first (full click) before you start the drag or you'll end up dragging whatever node was actually selected before the drag.

In real life, you'd have to come up with a scheme to make sure your keys across trees remain unique.  In this sample, it's no problem because I move items (rather than copy) and I start off with all keys unique across both trees.

The code is just a sample.  If I were actually doing it, I'd probably "subclass" the treeview and create a new ActiveX Control exposing those functions that I needed and basically encapsulating the whole thing--  you'll notice a lot of repeated code for each treeview control.

Option Compare Database
Option Explicit

Dim dragging As Integer

Dim WithEvents t1 As TreeView
Dim WithEvents t2 As TreeView

Private Sub Form_Load()
   
    Set t1 = TreeView1.Object
    Set t2 = TreeView2.Object
   
    t2.OLEDragMode = ccOLEDragAutomatic
    t1.OLEDragMode = ccOLEDragAutomatic
    t2.OLEDropMode = ccOLEDropManual
    t1.OLEDropMode = ccOLEDropManual
   
    t1.Nodes.Add , , "Node1", "Node 1"
    t1.Nodes.Add "Node1", tvwChild, "NodeA", "Node A"
    t2.Nodes.Add , , "Node2", "Node 2"
    t2.Nodes.Add "Node2", tvwChild, "NodeB", "Node B"
   
End Sub

Private Sub t1_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
   
    If Data.GetFormat(1) Then
        If Data.GetData(1) = "DragTree2" Then
            movenodes t2.SelectedItem, t2, t1.DropHighlight, t1
            t2.Nodes.Remove t2.SelectedItem.Key
            Set t1.DropHighlight = Nothing
        End If
    End If

End Sub

Private Sub t1_OLEDragOver(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single, State As Integer)
    'Don't Hilight the tree if it's dragging over itself
    If dragging <> 1 Then Set t1.DropHighlight = t1.HitTest(x, y)
End Sub

Private Sub t1_OLESetData(Data As MSComctlLib.DataObject, DataFormat As Integer)
    Data.SetData "DragTree1", 1
End Sub

Private Sub t1_OLEStartDrag(Data As MSComctlLib.DataObject, AllowedEffects As Long)
    dragging = 1
End Sub

Private Sub t2_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
   
    If Data.GetFormat(1) Then
        If Data.GetData(1) = "DragTree1" Then
            movenodes t1.SelectedItem, t1, t2.DropHighlight, t2
            t1.Nodes.Remove t1.SelectedItem.Key
            Set t2.DropHighlight = Nothing
        End If
    End If
       
End Sub

Private Sub t2_OLEDragOver(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single, State As Integer)
    If dragging <> 2 Then Set t2.DropHighlight = t2.HitTest(x, y)
End Sub

Private Sub t2_OLESetData(Data As MSComctlLib.DataObject, DataFormat As Integer)

    Data.SetData "DragTree2", 1
   
End Sub

Private Sub movenodes(nSource As Node, tSource As TreeView, nDest As Node, tDest As TreeView)
   
    Dim childNode As Node
    Dim removeNode As Node
    Dim newNode As Node
   
    If Not (IsNull(nSource)) Then
        If nDest Is Nothing Then
            Set newNode = tDest.Nodes.Add(, , nSource.Key, nSource.Text)
        Else
            Set newNode = tDest.Nodes.Add(nDest.Index, tvwChild, nSource.Key, nSource.Text)
        End If
       
    End If
   
    If nSource.Children > 0 Then
        Set childNode = nSource.Child
        Do
            Set removeNode = childNode
            movenodes childNode, tSource, newNode, tDest
            Set childNode = childNode.Next
            tSource.Nodes.Remove removeNode.Index
        Loop Until childNode Is Nothing
   
    End If
   
End Sub

Private Sub t2_OLEStartDrag(Data As MSComctlLib.DataObject, AllowedEffects As Long)
    dragging = 2
End Sub
0
 
LVL 12

Expert Comment

by:Trygve
ID: 1997817
Will it be possible to drag other objects (even from other programs) into the tree?
0
 
LVL 25

Expert Comment

by:clockwatcher
ID: 1997818
Trygve,

Yeah, it's no problem.  It won't work in the sample that I posted because I'm only using the OLE data to check whether the post came from the other tree.  But if you alter the Drop event, like so:

Private Sub t2_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
   
    Dim check
   
    If Data.GetFormat(1) Then
        check = Data.GetData(1)
        If check = "DragTree1" Then
            movenodes t1.SelectedItem, t1, t2.DropHighlight, t2
            t1.Nodes.Remove t1.SelectedItem.Key
            Set t2.DropHighlight = Nothing
        Else
            If t2.DropHighlight Is Nothing Then
                t2.Nodes.Add , , check, check
            Else
                t2.Nodes.Add t2.DropHighlight.Key, tvwChild, check, check
            End If
        End If
    End If
         
End Sub

You can now drag text data from other programs into TreeView2 and create new nodes.  Of course, if the text that you drag into the tree happens to be "DragTree1" it would cause problems ;-)
0
 
LVL 12

Expert Comment

by:Trygve
ID: 1997819
Thank you!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question