Solved

Treeview Drag and Drop

Posted on 2001-09-05
8
655 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month4 days, 13 hours left to enroll

636 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