Solved

Treeview Drag and Drop

Posted on 2001-09-05
8
642 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
[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
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

752 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