Solved

Treeview Drag and Drop

Posted on 2001-09-05
8
623 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to debug this code 7 56
Prevent checkbox click event occur while editing it in vb6 8 32
Visual Studio 2005 text editor 10 35
Advice in Xamarin 21 47
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

932 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

11 Experts available now in Live!

Get 1:1 Help Now