Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code not executed

Status
Not open for further replies.

curiousvbnet

Programmer
Apr 6, 2007
40
FR
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
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
 
I think that there is a property called AllowDragDrop?
If you enable this on the TreeView, it should work.
 
Hi,

Thanks a lot , it works better but it does not give any result; in fact the program stops just after the drag_over event but i will see with the debbuger where there is a pb .

Moreover , does this event(dragdrop), takes in consideration that in the database, now there will be a new relation parent/child because of a node copy in the treeview1).
Because at the beginning, the treeview is built with the help of this table , using the relation parent/child to buit the treeview.

Thanks again

Regards.

Nathalie

 
I have just spotted in your line:
Code:
If e.Data.GetDataPresent("system.Windows.Forms.TreeNode", False) Then
   Exit Sub
Should it be:
Code:
If Not e.Data.GetDataPresent("system.Windows.Forms.TreeNode", False) Then
   Exit Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top