curiousvbnet
Programmer
Hi,
I try to make an event dragdrop work in a treeview called Treeview1, but i can see that when i select an item in the treeview, to copy it at another place ,the code below is not executed at all.
Only the Treeview1_ItemDrag is considered.
Could you explain me what happens?
Thanks a lot for your help.
Regards
Nathalie
Here is the code i use
I try to make an event dragdrop work in a treeview called Treeview1, but i can see that when i select an item in the treeview, to copy it at another place ,the code below is not executed at all.
Only the Treeview1_ItemDrag is considered.
Could you explain me what happens?
Thanks a lot for your help.
Regards
Nathalie
Here is the code i use
Code:
[blue] Private Sub TreeView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles TreeView1.ItemDrag[/blue]
DoDragDrop(e.Item, DragDropEffects.Copy)
End Sub
'on teste que les données d'arrivée sont bien des treenode(Treeview1 est ici le contrôle d'arrivée); si c'est le cas la copie du noeud est exéctée
[blue] Private Sub TreeView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragEnter[/blue]
If e.Data.GetDataPresent("System.Windows.Forms.TreeNode", True) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
'cet évènement survient quand la souris se balade sur le contrôle d'arrivée; il vérifie si le drop reçoit bien un noeud
'pt :e.X et e.Y sont les coordonées écran : on ls transforme en coordonnées du contrôle client, cad du treeview1(contrôle d'arrivée)
[blue]
Private Sub TreeView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragOver[/blue]
Dim pt As Point, NodeCible As TreeNode, NodeSource As TreeNode
If e.Data.GetDataPresent("system.Windows.Forms.TreeNode", False) Then
Exit Sub
Else
pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
NodeCible = Me.TreeView1.GetNodeAt(pt)
NodeSource = CType(e.Data.GetData("System.Windows.Forms.TreeNode"), TreeNode)
If Not (Me.TreeView1.SelectedNode Is NodeCible) Then
Me.TreeView1.SelectedNode = NodeCible
End If 'fin de If Not (Me.TreeView1.SelectedNode Is NodeCible)
Do Until NodeCible Is Nothing
If NodeCible Is NodeSource Then
e.Effect = DragDropEffects.None
Exit Sub
End If 'fin de If NodeCible Is NodeSource Then
NodeCible = NodeCible.Parent
Loop
e.Effect = DragDropEffects.Copy
End If 'fin de If e.Data.GetDataPresent("system.Windows.Forms.TreeNode", False)
End Sub
'dans cette procédure on utilise la méthode GetData pour extraire les données que l'on fait glisser.
[blue] Private Sub TreeView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragDrop[/blue]
Dim nodesource As TreeNode
Dim nodecible As TreeNode
Dim verifparent As TreeNode
If e.Data.GetDataPresent("system.Windows.Forms.TreeNode", False) Then
Exit Sub
Else
nodesource = CType(e.Data.GetData("system.Windows.Forms.TreeNode"), TreeNode)
nodecible = TreeView1.SelectedNode
verifparent = nodecible
Do Until verifparent Is Nothing
If verifparent Is nodesource Then
Exit Sub
verifparent = verifparent.Parent
End If
Loop
nodesource.Remove()
'si il n'y a pas de noeud cible alors on ajooute le noeud source au treeview, en fait on considère que le noeud cible est le noeud source.
'si le neoud cible existe , on lui ajoute le noeud spource que l'on a copié
'le noeud copié (noeud source) devient le noeud sélectionné
If nodecible Is Nothing Then
TreeView1.Nodes.Add(nodesource)
Else
nodecible.Nodes.Add(nodesource)
End If
nodesource.EnsureVisible()
TreeView1.SelectedNode = nodesource
End If
End Sub