Solved

Treeview Drag and Drop

Posted on 2001-09-05
8
621 Views
Last Modified: 2011-10-03
I'm using VB 6.0 and am trying to do drag-and-drop with a treeview control.  If you look at the folders section in Windows Explorer you'll be able to see what I'm trying to do.  Basically, I want the user to click on a node in the treeview control and be able to drag that node over top of another node and move it there by releasing the mouse button.  Just like in Windows Explorer.  I've done drag-and-drop before so I know the basics of using the Drag, DragOver, and DragDrop methods, I just don't know how to drag a node from a treeview control to another node in the same treeview control.

Please Help.

- Alex
0
Comment
Question by:Egore
8 Comments
 
LVL 5

Expert Comment

by:rkot2000
ID: 6458818
i think you need to set a parent node. I have a sample at home I will post tomorrow.
0
 
LVL 1

Expert Comment

by:johnczimm
ID: 6458841
Take a look at this program I wrote awhile back.  I think it will do what you are looking for.
http://home.earthlink.net/~jczimmerman/explorerStyleApp.zip
0
 
LVL 1

Expert Comment

by:johnczimm
ID: 6458845
0
 
LVL 5

Accepted Solution

by:
Julian_K earned 200 total points
ID: 6458942
' Here's an example of how do I do this...
' Don't forget to set the treeview's "MouseIcon" property...

Private nodDragged As Node
Private blnDragging As Boolean

Private Sub TreeView1_DragDrop(Source As Control, x As Single, y As Single)
    On Error GoTo L_ErrHandler
   
    'if not dropped over any of the nodes or over itself, then drop the node
    If Not (TreeView1.DropHighlight Is Nothing) Then
        If Not (nodDragged Is TreeView1.DropHighlight) Then
            Set nodDragged.Parent = TreeView1.DropHighlight
        End If
    End If
   
    'release objects
    Set TreeView1.DropHighlight = Nothing
    Set nodDragged = Nothing
    blnDragging = False
   
Exit Sub
'If trying to drop the node over it's own child...
L_ErrHandler:
    If Err.Number = 35614 Then
        MsgBox "Circular reference not allowed!"
        Resume Next
    End If
End Sub

'Lit the node beneath the mouse
Private Sub TreeView1_DragOver(Source As Control, x As Single, y As Single, State As Integer)
    Set TreeView1.DropHighlight = TreeView1.HitTest(x, y)
End Sub

'prepare to move the node.
Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    If Button = 1 Then
        Set nodDragged = TreeView1.HitTest(x, y)
    Else
        Set nodDragged = Nothing
    End If
End Sub

'initialize drag-drop process
Private Sub TreeView1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    If blnDragging Then Exit Sub
    If Button <> 1 Then Exit Sub
    TreeView1.DragIcon = TreeView1.MouseIcon
    blnDragging = True
    TreeView1.Drag vbBeginDrag
End Sub
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 3

Expert Comment

by:adg
ID: 6459101
Learning
0
 
LVL 4

Expert Comment

by:VincentLawlor
ID: 6459128
Listening...
0
 
LVL 3

Author Comment

by:Egore
ID: 6459273
You all started posting about 5 minutes after I left the office, argh! :)  Anyway, I did this a couple years back and Julian K's answer looks right.  I will be implementing this tomorrow morning, so I will definately give the thumbs up or thumbs down by noon (EST) tomorrow.

Thanks!

- Alex
0
 
LVL 3

Author Comment

by:Egore
ID: 6460566
This worked like an absolute charm!  I changed the name of the TreeView control and it worked without a hitch, thanks!

- Alex
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

757 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now