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

Windows Forms Discussion :

[VB.NET] Drag and drop entre 2 treeviews


Sujet :

Windows Forms

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    38
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 38
    Points : 21
    Points
    21
    Par défaut [VB.NET] Drag and drop entre 2 treeviews
    Bonjour,

    Je cherche à déplacer des noeuds d'un treeview vers un autre (ca j'arrive) mais j'ai un problème :

    ex :

    A
    1
    2
    aa
    bb
    3

    Imaginons que souhaite déplacer le noeud 2 vers le treeview de destination, je souhaite qu'il prenne en mémoire ses parents et ses enfants ( donc le noeud A , le 2 et aa et bb)
    Du coup je me retrouverais dans cette situation :

    A A
    1 2
    3 aa
    bb

    Il sera possible du coup de déplacer la partie droite de la même manière sur la partie gauche en gardant l’arborescence.

  2. #2
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour jorrie...

    Pas bien compris le souci mais si c'est pour deplacer un noeud et son arborescence complete(descendance) soit dans le meme treeview soit d'un treeview à un autre.......
    voici un 1er exemple de la SDK qui deplace un noeud (et sa descendance complete) tout en verifiant bien sur s'il n'existe pas deja dans le TreeView de destination:
    explicatif sdk:
    L'exemple de code suivant illustre l'activation d'opérations glisser-déplacer dans un contrôle TreeView. Dans cet exemple, il est possible de faire glisser tout nœud vers un autre nœud qui n'est pas descendant du nœud qui glisse. Le nœud qui glisse, y compris tous ses nœuds descendants, devient un enfant du nœud de destination. Lorsque le bouton gauche de la souris est utilisé, le nœud qui glisse est déplacé vers le nœud de destination. Lorsque le bouton droit de la souris est utilisé, le nœud qui glisse est copié au nœud de destination

    1er exemple:
    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
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
     
     
    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
     
    Public Class FormTreeViewDragDrop
     
        Private treeView1 As TreeView
        Public Sub New()
     
            ' Cet appel est requis par le concepteur.
            InitializeComponent()
     
            ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
     
     
            treeView1 = New TreeView()
     
            Me.SuspendLayout()
     
            ' Initialize treeView1.
            treeView1.AllowDrop = True
            treeView1.Dock = DockStyle.Fill
     
            ' Add nodes to treeView1.
            Dim node As TreeNode
            Dim x As Integer
            For x = 0 To 3
                ' Add a root node to treeView1.
                node = treeView1.Nodes.Add(String.Format("Node{0}", x * 4))
                Dim y As Integer
                For y = 1 To 4
                    ' Add a child node to the previously added node.
                    node = node.Nodes.Add(String.Format("Node{0}", x * 4 + y))
                Next y
            Next x
     
            ' Add event handlers for the required drag events.
            AddHandler treeView1.ItemDrag, AddressOf treeView1_ItemDrag
            AddHandler treeView1.DragEnter, AddressOf treeView1_DragEnter
            AddHandler treeView1.DragOver, AddressOf treeView1_DragOver
            AddHandler treeView1.DragDrop, AddressOf treeView1_DragDrop
     
            ' Initialize the form.
            Me.ClientSize = New Size(292, 273)
            Me.Controls.Add(treeView1)
     
            Me.ResumeLayout(False)
        End Sub 'New
     
     
        Private Sub treeView1_ItemDrag(ByVal sender As Object, ByVal e As ItemDragEventArgs)
     
            ' 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)
            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)
            ' 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)
     
            ' 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
     
    End Class
    2eme exemple c'est le 1er reamanege avec un treeView2 en plus et les handlers modifies _DragEnter,treeView2treeView2_DragOver,treeView2_DragDrop......

    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
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
     
     
     
     
    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
     
    Public Class FormTwoTreeViewDragDrop
     
        Private treeView1 As TreeView
        Private treeView2 As TreeView
        Public Sub New()
     
            ' Cet appel est requis par le concepteur.
            InitializeComponent()
     
            ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
     
     
            treeView1 = New TreeView()
            treeView2 = New TreeView()
            Me.SuspendLayout()
     
            ' Initialize treeView1.
            treeView2.AllowDrop = True
            treeView1.Dock = DockStyle.Left
            treeView2.Dock = DockStyle.Right
            ' Add nodes to treeView1.
            Dim node As TreeNode
            Dim x As Integer
            For x = 0 To 3
                ' Add a root node to treeView1.
                node = treeView1.Nodes.Add(String.Format("Node{0}", x * 4))
                Dim y As Integer
                For y = 1 To 4
                    ' Add a child node to the previously added node.
                    node = node.Nodes.Add(String.Format("Node{0}", x * 4 + y))
                Next y
            Next x
            ' Add nodes to treeView2.
     
     
            For x = 0 To 3
                ' Add a root node to treeView2.
                node = treeView2.Nodes.Add(String.Format("Node{0}", x * 5))
                Dim y As Integer
                For y = 1 To 4
                    ' Add a child node to the previously added node.
                    node = node.Nodes.Add(String.Format("Node{0}", x * 5 + y))
                Next y
            Next x
            ' Add event handlers treeView1 for the required drag events.
            AddHandler treeView1.ItemDrag, AddressOf treeView1_ItemDrag
     
            ' Add event handlers treeView2 for the required drag events.
            AddHandler treeView2.DragEnter, AddressOf treeView2_DragEnter
            AddHandler treeView2.DragOver, AddressOf treeView2_DragOver
            AddHandler treeView2.DragDrop, AddressOf treeView2_DragDrop
     
            ' Initialize the form.
            Me.ClientSize = New Size(292, 273)
            Me.Controls.Add(treeView1)
            Me.Controls.Add(treeView2)
            Me.ResumeLayout(False)
        End Sub 'New
     
     
        Private Sub treeView1_ItemDrag(ByVal sender As Object, ByVal e As ItemDragEventArgs)
     
            ' 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 treeView2_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
            e.Effect = e.AllowedEffect
        End Sub 'treeView2_DragEnter
     
        ' Select the node under the mouse pointer to indicate the 
        ' expected drop location.
        Private Sub treeView2_DragOver(ByVal sender As Object, ByVal e As DragEventArgs)
            ' Retrieve the client coordinates of the mouse position.
            Dim targetPoint As Point = treeView2.PointToClient(New Point(e.X, e.Y))
     
            ' Select the node at the mouse position.
            treeView2.SelectedNode = treeView2.GetNodeAt(targetPoint)
        End Sub 'treeView2_DragOver
     
        Private Sub treeView2_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
     
            ' Retrieve the client coordinates of the drop location.
            Dim targetPoint As Point = treeView2.PointToClient(New Point(e.X, e.Y))
     
            ' Retrieve the node at the drop location.
            Dim targetNode As TreeNode = treeView2.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 'treeView2_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
     
    End Class 'Form1
    en esperant qu'ils repondent au souci....................
    bon code..................

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    38
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 38
    Points : 21
    Points
    21
    Par défaut
    Merci de m'avoir répondu , mon soucie est de pouvoir déplacer un noeud d'un treeview vers un autre.

    Du coup il faut que je conserve son amont et aval pour avoir la même arborescence dans les deux treeview .

    Chaque déplacement supprime les informations pour les replacer dans l'autre treeview sauf si chacun des noeuds déplacer conserve encore des enfants.

  4. #4
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    Rebonjour jorrie...
    Pas bien compris mais un drag drop peut faire soit un move soit un copy(bouton gauche soit bouton droit comme explique par msdn dans ce code)....

    Apres le choix copy tu n'as qu'à deleter le draggedNode dans le 1er treeview...
    bon code.....

  5. #5
    Membre à l'essai
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    38
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 38
    Points : 21
    Points
    21
    Par défaut
    J'utilise pas le dragdrop pour transférer d'un treeview à un autre mais je check les noeuds qui m'intèresse puis je click sur un bouton qui transfère les noeuds et leur arborescence dans le treeview qu'il désire

  6. #6
    Membre à l'essai
    Profil pro
    Inscrit en
    Septembre 2007
    Messages
    38
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2007
    Messages : 38
    Points : 21
    Points
    21
    Par défaut
    Je vais refaire en plus clair :

    J'ai un treeview initiale de type :

    A
    1
    2
    a
    b
    B
    3

    J'aimerais par exemple déplacer le noeud "2" vers un second treeview, du coup j'aurais :

    A
    1
    B
    3

    Et le 2ème treeview
    A
    2
    a
    b

    Ensuite je sauvegarde le treeview n°2, ainsi je peux le recharger quand je le désire.
    On peut aussi déplacer les noeuds du treeview 2 vers le n°1

    Ma question est donc, comment puis déplacer rapidement les noeuds d'un treeview à un autre sachant que je garder l'arborescence du treeview initial.

    En gros si du treeview 2
    A
    2
    a
    b
    je déplace le noeud "a" , je veux que ce noeud ce replace au bon endroit dans le n°1 : pour avoir

    A
    1
    2
    a
    B
    3

    et pour le n°2
    A
    2
    b


    Moi ce que je pense c'est pour le noeud sélectionner, je remonte son arborescence pour abtenir tout c'est parent, ainsi lors de l'ajout de le nouveau treeview je descend l'arborence du noeud en testant l'existance de chaque noeud pour éviter le double ajout

Discussions similaires

  1. [ASP.NET][C#] Drag and Drop entre 2 gridview
    Par hurricane dans le forum ASP.NET
    Réponses: 1
    Dernier message: 16/01/2008, 08h55
  2. [VB.net] Drag and drop dans une Treeview
    Par gégécap dans le forum Windows Forms
    Réponses: 2
    Dernier message: 19/10/2006, 10h05
  3. [Débutant(e)][VB.NET] Drag and drop entre 2 treeviews
    Par - Manuella Leray - dans le forum Windows Forms
    Réponses: 8
    Dernier message: 13/10/2005, 15h54
  4. [FLASH MX2004] Drag and drop entre deux List
    Par aldo-tlse dans le forum Flash
    Réponses: 15
    Dernier message: 24/09/2005, 01h10
  5. Drag and Drop entre listbox
    Par zwoke dans le forum C++Builder
    Réponses: 2
    Dernier message: 05/07/2004, 14h10

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