Exemple permettant de montrer le drag and drop (glisser déplcer) de répertoire ou de fichiers depuis l'explorateur de window

Description

Exemple permettant de montrer le drag and drop (glisser déplcer) de répertoire ou de fichiers depuis l'explorateur de windows

!! Attention !!
il ne faut pas oublier d'affecter la propriété "AllowDrop" à "True" de votre "TreeView".

Source / Exemple :


''' <summary>
    ''' Permet la gestion du Drag and Drop
    ''' </summary>
    Private Sub trvTreeList_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles trvTreeList.DragDrop
        Dim arr As Array = CType(e.Data.GetData(DataFormats.FileDrop), Array)

        For i As Integer = 0 To arr.Length - 1
            Dim path As String = TryCast(arr.GetValue(i), String)
            If File.Exists(path) Then
                trvTreeList.Nodes.Add(path)
            ElseIf Directory.Exists(path) Then
                Dim di As New DirectoryInfo(path)
                Dim sRootPath As String = String.Empty
                Dim treeNode As TreeNode

                If di.Parent.Name <> "" Then
                    treeNode = trvTreeList.Nodes.Add(di.FullName, di.Name, 0, 1)
                Else
                    treeNode = trvTreeList.Nodes(0)
                End If

                ' Exécute une boucle récursive permettant de remplir le TreeView avec la liste des fichiers
                PopulateFromFolderToTreeView(path, treeNode)
            End If
        Next

    End Sub

    ''' <summary>
    ''' Permet la gestion du Drag and Drop
    ''' </summary>
    Private Sub trvTreeList_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles trvTreeList.DragEnter
        'e.Effect = If(e.Data.GetDataPresent(DataFormats.FileDrop), DragDropEffects.Copy, DragDropEffects.None)
        e.Effect = e.AllowedEffect
    End Sub

    ''' <summary>
    ''' Permet la gestion du Drag and Drop
    ''' </summary>
    Private Sub trvTreeList_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles trvTreeList.DragOver
        ' Rubrique pour le déplacer dans un TreeView
        ' Retrieve the client coordinates of the mouse position.
        Dim targetPoint As Point = trvTreeList.PointToClient(New Point(e.X, e.Y))
        ' Select the node at the mouse position.
        trvTreeList.SelectedNode = trvTreeList.GetNodeAt(targetPoint)
    End Sub

    ''' <summary>
    ''' Permet la gestion du Drag and Drop
    ''' </summary>
    Private Sub trvTreeList_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles trvTreeList.ItemDrag
        ' Move the dragged node when the left mouse button is used.
        If e.Button = MouseButtons.Left Then DoDragDrop(e.Item, DragDropEffects.Move)
    End Sub

    Public Sub PopulateFromFolderToTreeView(ByVal dir As String, ByVal node As TreeNode)
        ' get the information of the directory
        Dim directory As New DirectoryInfo(dir)

        ' Teste si le fichier ou le répertoire est en accès refusé ou autre
        Try
            directory.GetDirectories()
        Catch ex As Exception
            MsgBox(ex.Message & " the program continue without this file !", MsgBoxStyle.Critical, Application.ProductName)
            Exit Sub
        End Try

        ' loop through each subdirectory
        For Each d As DirectoryInfo In directory.GetDirectories()
            ' create a new node
            Dim t As New TreeNode(d.Name, 1, 1)
            t.Name = d.FullName
            Dim tmp As String = d.Parent.Name

            ' populate the new node recursively
            PopulateFromFolderToTreeView(d.FullName, t)
            node.Nodes.Add(t) ' add the node to the "master" node
            'node.Nodes.Add(d.FullName, d.Name) ' add the node to the "master" node
        Next d
        ' lastly, loop through each file in the directory, and add these as nodes
        For Each f As FileInfo In directory.GetFiles()
            ' create a new node
            'Dim t As New TreeNode(f.Name)
            '' add it to the "master"
            'node.Nodes.Add(t)

            Dim t As New TreeNode(f.Name, 2, 2)
            ' add it to the "master"
            'node.Nodes.Add(t)
            node.Nodes.Add(f.FullName, f.Name, 2, 2)
        Next f
    End Sub

Conclusion :


En fait c'est tout simple non ?

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.