CodeItBetter Programming Another VB Programming Blog

How to implement Drag and Drop in TreeView Control

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'Controls - How to implement Drag and Drop in TreeView Control

Option Explicit
 
'Drag and drop one node to another. The following code will pop up message box with
'the name of the node that been dragged. This will not let the user drag a parent node.

'Add a TreeView Control and Set the TreeView's OLEDragMode property to 1 - ccOLEDragAutomation,
'OLEDropMode property to 1 - ccOLEDropManual, LineStyle property to 1 -tvwRootLines.

Public dragNode As Node, hilitNode As Node
 
Private Sub Form_Load()
    'the following code lines will populate the TreeView control
   TreeView1.Nodes.Add , , "First", "First"
   TreeView1.Nodes.Add , , "Second", "Second"
   TreeView1.Nodes.Add "First", tvwChild, "Child", "Child"
   TreeView1.Nodes.Add "Child", tvwChild, "Child2", "Child2"
End Sub
 
Private Sub TreeView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
   Set dragNode = TreeView1.HitTest(x, y)
End Sub
 
Private Sub TreeView1_OLEDragDrop(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single)
   If Not dragNode Is Nothing Then MsgBox (dragNode.Text)
End Sub
 
Private Sub TreeView1_OLEStartDrag(Data As MSComctlLib.DataObject, AllowedEffects As Long)
    'If you want to allow parent node dragging, delete the line below
    If dragNode.Parent Is Nothing Then Set dragNode = Nothing
End Sub
 
Private Sub TreeView1_OLEDragOver(Data As MSComctlLib.DataObject, Effect As Long, Button As Integer, Shift As Integer, x As Single, y As Single, State As Integer)
    If Not dragNode Is Nothing Then
        TreeView1.DropHighlight = TreeView1.HitTest(x, y)
    End If
End Sub
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.