IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Problème avec Drag And Drop TreeWiew


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre actif
    Homme Profil pro
    Informatique, programmation
    Inscrit en
    Novembre 2010
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Vienne (Poitou Charente)

    Informations professionnelles :
    Activité : Informatique, programmation

    Informations forums :
    Inscription : Novembre 2010
    Messages : 33
    Par défaut Problème avec Drag And Drop TreeWiew
    Salut,

    J'ai trouvé un exemple sur le forum MDSN sur le drag and drop avec un TreeWiew.
    Ceci fonctionne très bien si ce n'est que si je drag and drop tous en bas du TreeWiew et bien j'ai
    une erreur:

    L'exception System.NullReferenceException n'a pas été gérée par le code utilisateur
    HResult=-2147467261
    Message=La référence d'objet n'est pas définie à une instance d'un objet.
    Source=WindowsApplication1
    StackTrace:
    à WindowsApplication1.FenêtreMenuMaker.ContainsNode(TreeNode node1, TreeNode node2) dans C:\Users\Dayvid\AppData\Local\Temporary Projects\WindowsApplication1\Fenêtre Menu Maker.vb:ligne 80
    à WindowsApplication1.FenêtreMenuMaker.treeView1_DragDrop(Object sender, DragEventArgs e) dans C:\Users\Dayvid\AppData\Local\Temporary Projects\WindowsApplication1\Fenêtre Menu Maker.vb:ligne 52
    à System.Windows.Forms.Control.OnDragDrop(DragEventArgs drgevent)
    à System.Windows.Forms.Control.System.Windows.Forms.IDropTarget.OnDragDrop(DragEventArgs drgEvent)
    à System.Windows.Forms.DropTarget.System.Windows.Forms.UnsafeNativeMethods.IOleDropTarget.OleDrop(Object pDataObj, Int32 grfKeyState, POINTSTRUCT pt, Int32& pdwEffect)
    InnerException:


    Je suppose que c'est parce que je drag and drop sur rien ?
    Voici le code, il vous faudra créer une fenêtre avec un TreeWiew, ajouter des éléments dans
    celui-ci puis exécutez, drag and drop un item vers le bas ou ya rien et la magie -> erreur :'(
    Coller ce code dans votre classe form.

    Je vous remercie de votre aide par avance.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    Private Sub treeView1_ItemDrag(ByVal sender As Object, ByVal e As ItemDragEventArgs) Handles TreeView1.ItemDrag
     
            ' Move the dragged node when the left mouse button is used.
            If e.Button = MouseButtons.Left Then
                DoDragDrop(e.Item, DragDropEffects.Move)
     
                ' Copy the dragged node when the right mouse button is used.
            ElseIf e.Button = MouseButtons.Right Then
                DoDragDrop(e.Item, DragDropEffects.Copy)
     
            End If
     
        End Sub 'treeView1_ItemDrag
     
        ' Set the target drop effect to the effect 
        ' specified in the ItemDrag event handler.
        Private Sub treeView1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragEnter
            e.Effect = e.AllowedEffect
        End Sub 'treeView1_DragEnter
     
        ' Select the node under the mouse pointer to indicate the 
        ' expected drop location.
        Private Sub treeView1_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragOver
     
            ' Retrieve the client coordinates of the mouse position.
            Dim targetPoint As Point = TreeView1.PointToClient(New Point(e.X, e.Y))
     
            ' Select the node at the mouse position.
            TreeView1.SelectedNode = TreeView1.GetNodeAt(targetPoint)
     
        End Sub 'treeView1_DragOver
     
        Private Sub treeView1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragDrop
     
            ' Retrieve the client coordinates of the drop location.
            Dim targetPoint As Point = TreeView1.PointToClient(New Point(e.X, e.Y))
     
            ' Retrieve the node at the drop location.
            Dim targetNode As TreeNode = TreeView1.GetNodeAt(targetPoint)
     
            ' Retrieve the node that was dragged.
            Dim draggedNode As TreeNode = CType(e.Data.GetData(GetType(TreeNode)), TreeNode)
     
            ' Confirm that the node at the drop location is not 
            ' the dragged node or a descendant of the dragged node.
            If Not draggedNode.Equals(targetNode) AndAlso Not ContainsNode(draggedNode, targetNode) Then
     
                ' If it is a move operation, remove the node from its current 
                ' location and add it to the node at the drop location.
                If e.Effect = DragDropEffects.Move Then
                    draggedNode.Remove()
                    targetNode.Nodes.Add(draggedNode)
     
                    ' If it is a copy operation, clone the dragged node 
                    ' and add it to the node at the drop location.
                ElseIf e.Effect = DragDropEffects.Copy Then
                    targetNode.Nodes.Add(CType(draggedNode.Clone(), TreeNode))
     
                End If
     
                ' Expand the node at the location 
                ' to show the dropped node.
                targetNode.Expand()
     
            End If
     
        End Sub 'treeView1_DragDrop
     
        ' Determine whether one node is a parent 
        ' or ancestor of a second node.
        Private Function ContainsNode(ByVal node1 As TreeNode, ByVal node2 As TreeNode) As Boolean
     
            ' Check the parent node of the second node.
            If node2.Parent Is Nothing Then
                Return False
            End If
     
            If node2.Parent.Equals(node1) Then
                Return True
            End If
     
            ' If the parent node is not null or equal to the first node, 
            ' call the ContainsNode method recursively using the parent of 
            ' the second node.
            Return ContainsNode(node1, node2.Parent)
     
        End Function 'ContainsNode

  2. #2
    Membre très actif
    Profil pro
    Inscrit en
    Avril 2013
    Messages
    267
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2013
    Messages : 267
    Par défaut
    Bonsoir,

    voici une solution qui marche avec un ListBox:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Private Sub ListBox1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
            If e.Data.GetDataPresent(DataFormats.FileDrop) Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.None
            End If
        End Sub
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Private Sub ListBox1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
            Dim data As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
            For Each str In data
                'Ton code pour lire ton fichier avec treeview
            Next
        End Sub
    ce code fonction car je l'applique avec une application.
    bonsoir,

  3. #3
    Membre actif
    Homme Profil pro
    Informatique, programmation
    Inscrit en
    Novembre 2010
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Vienne (Poitou Charente)

    Informations professionnelles :
    Activité : Informatique, programmation

    Informations forums :
    Inscription : Novembre 2010
    Messages : 33
    Par défaut
    Bonjour, merci de ton aide mais là ça ne fonctionne pas pour moi.
    Moi, le drag et drop, c'est pour réorganiser les éléments ou les copier...

    Si je lâche l’élément sur "Rien", c'est ici qu'intervient le problème.

  4. #4
    Membre très actif
    Profil pro
    Inscrit en
    Avril 2013
    Messages
    267
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2013
    Messages : 267
    Par défaut
    Bonjour,

    Regarde ce lien c'est peut être la solution que du chercher :
    http://www.telerik.com/help/winforms...-and-drop.html

    cordialement,

  5. #5
    Membre actif
    Homme Profil pro
    Informatique, programmation
    Inscrit en
    Novembre 2010
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Vienne (Poitou Charente)

    Informations professionnelles :
    Activité : Informatique, programmation

    Informations forums :
    Inscription : Novembre 2010
    Messages : 33
    Par défaut
    Merci de ton aide.

    Alors je ne sais pas si c'est bien mais j'ai fais comme ça, c'est du bricolage mais ça marche:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
     Private Sub treeView1_ItemDrag(ByVal sender As Object, ByVal e As ItemDragEventArgs) Handles TreeView1.ItemDrag
     
            ' Move the dragged node when the left mouse button is used.
            If e.Button = MouseButtons.Left Then
                DoDragDrop(e.Item, DragDropEffects.Move)
     
                ' Copy the dragged node when the right mouse button is used.
            ElseIf e.Button = MouseButtons.Right Then
                DoDragDrop(e.Item, DragDropEffects.Copy)
     
            End If
     
        End Sub 'treeView1_ItemDrag
     
        ' Set the target drop effect to the effect 
        ' specified in the ItemDrag event handler.
        Private Sub treeView1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragEnter
            e.Effect = e.AllowedEffect
        End Sub 'treeView1_DragEnter
     
        ' Select the node under the mouse pointer to indicate the 
        ' expected drop location.
        Private Sub treeView1_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragOver
     
            ' Retrieve the client coordinates of the mouse position.
            Dim targetPoint As Point = TreeView1.PointToClient(New Point(e.X, e.Y))
     
            ' Select the node at the mouse position.
            TreeView1.SelectedNode = TreeView1.GetNodeAt(targetPoint)
     
        End Sub 'treeView1_DragOver
     
        Private Sub treeView1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles TreeView1.DragDrop
     
            ' Retrieve the client coordinates of the drop location.
            Dim targetPoint As Point = TreeView1.PointToClient(New Point(e.X, e.Y))
     
            ' Retrieve the node at the drop location.
            Dim targetNode As TreeNode = TreeView1.GetNodeAt(targetPoint)
     
            ' Retrieve the node that was dragged.
            Dim draggedNode As TreeNode = CType(e.Data.GetData(GetType(TreeNode)), TreeNode)
     
            ' Confirm that the node at the drop location is not 
            ' the dragged node or a descendant of the dragged node.
            If Not draggedNode.Equals(targetNode) AndAlso Not ContainsNode(draggedNode, targetNode) Then
     
                If Not targetNode Is Nothing Then
     
                    ' If it is a move operation, remove the node from its current 
                    ' location and add it to the node at the drop location.
                    If e.Effect = DragDropEffects.Move Then
                        draggedNode.Remove()
                        targetNode.Nodes.Add(draggedNode)
     
                        ' If it is a copy operation, clone the dragged node 
                        ' and add it to the node at the drop location.
                    ElseIf e.Effect = DragDropEffects.Copy Then
                        targetNode.Nodes.Add(CType(draggedNode.Clone(), TreeNode))
     
                    End If
     
                    ' Expand the node at the location 
                    ' to show the dropped node.
                    targetNode.Expand()
     
                End If
     
            End If
     
        End Sub 'treeView1_DragDrop
     
        ' Determine whether one node is a parent 
        ' or ancestor of a second node.
        Private Function ContainsNode(ByVal node1 As TreeNode, ByVal node2 As TreeNode) As Boolean
     
            If Not node2 Is Nothing Then
     
                ' Check the parent node of the second node.
                If node2.Parent Is Nothing Then
                    Return False
                End If
     
                If node2.Parent.Equals(node1) Then
                    Return True
                End If
     
                ' If the parent node is not null or equal to the first node, 
                ' call the ContainsNode method recursively using the parent of 
                ' the second node.
                Return ContainsNode(node1, node2.Parent)
     
            Else
                Return 0
            End If
     
        End Function 'ContainsNode
     
        Private Sub ButtonSupprimerElements_Click(sender As System.Object, e As System.EventArgs) Handles ButtonSupprimerElements.Click
            TreeView1.Nodes.Remove(TreeView1.SelectedNode)
        End Sub

  6. #6
    Membre actif
    Homme Profil pro
    Informatique, programmation
    Inscrit en
    Novembre 2010
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Vienne (Poitou Charente)

    Informations professionnelles :
    Activité : Informatique, programmation

    Informations forums :
    Inscription : Novembre 2010
    Messages : 33
    Par défaut
    Bonjour,

    Est-ce que quelqu'un peut me dire si ma méthode est bonne ou pas ?
    Merci

Discussions similaires

  1. Problème avec Drag and Drop WPF
    Par Moh1267 dans le forum C#
    Réponses: 2
    Dernier message: 04/06/2014, 11h35
  2. Problème de drag and drop
    Par te-san dans le forum ActionScript 3
    Réponses: 0
    Dernier message: 30/08/2010, 10h28
  3. Problème de Drag and Drop
    Par totodof dans le forum Windows Presentation Foundation
    Réponses: 18
    Dernier message: 13/04/2010, 07h54
  4. Problème de Drag and Drop
    Par superjaja dans le forum Débuter
    Réponses: 4
    Dernier message: 16/06/2009, 09h20
  5. problème avec drag and drop
    Par Nayra dans le forum Agents de placement/Fenêtres
    Réponses: 1
    Dernier message: 16/04/2009, 00h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo