locked
DataListView DragDrop/DragLeave RRS feed

  • Question

  • Hey,

    I wanna select some files & folders and drag them to a WinForms DataListView.

    Which event is the right one to get the names of selected files/folders dragged into the DataListView?

    DragDrop or DragLeave or else?

    And how to get the names of files/folders then?

    Thanks for the help indeed :)

    Friday, May 1, 2020 4:12 PM

Answers

  • You start with DragEnter e.g.

    Although not a DataGridView I wrote the following against a PictureBox in C#, translated it to VB

    Private Sub FilePictureBox_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
    	If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    		e.Effect = DragDropEffects.Copy
    	End If
    End Sub

    Then

    Private Sub FilePictureBox_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
    	Dim files = CType(e.Data.GetData(DataFormats.FileDrop), String())
    
    	For Each file In files
    		Dim fileName = Path.GetFileName(file)
    		If fileName.StartsWith(FileOperations.BaseFileNameWithoutExtension, StringComparison.InvariantCultureIgnoreCase) AndAlso Path.GetExtension(file).ToLower() = ".txt" Then
    			MessageBox.Show($"Accepted {fileName}" & vbLf & "But this is a WIP")
    		End If
    	Next file
    End Sub

    So the MessageBox in your case is where you logic goes to perform say a new row in your DataGridView.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 1, 2020 4:42 PM

All replies

  • Hi

    Here isone way.

    ' Blank Form1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim WithEvents LV As New ListView
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With LV
          .Location = New Point(5, 5)
          .Width = ClientSize.Width - 10
          .Height = ClientSize.Height - 80
          .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
          .View = View.Details
          .AllowDrop = True
          .Columns.Add("File Path", 400)
        End With
        Controls.Add(LV)
      End Sub
      Private Sub LV_DragDrop(sender As Object, e As DragEventArgs) Handles LV.DragDrop
        Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
        For Each s As String In files
          LV.Items.Add(s)
        Next
      End Sub
      Private Sub LV_DragEnter(sender As Object, e As DragEventArgs) Handles LV.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
          e.Effect = DragDropEffects.Copy
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Friday, May 1, 2020 4:38 PM
  • You start with DragEnter e.g.

    Although not a DataGridView I wrote the following against a PictureBox in C#, translated it to VB

    Private Sub FilePictureBox_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
    	If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    		e.Effect = DragDropEffects.Copy
    	End If
    End Sub

    Then

    Private Sub FilePictureBox_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
    	Dim files = CType(e.Data.GetData(DataFormats.FileDrop), String())
    
    	For Each file In files
    		Dim fileName = Path.GetFileName(file)
    		If fileName.StartsWith(FileOperations.BaseFileNameWithoutExtension, StringComparison.InvariantCultureIgnoreCase) AndAlso Path.GetExtension(file).ToLower() = ".txt" Then
    			MessageBox.Show($"Accepted {fileName}" & vbLf & "But this is a WIP")
    		End If
    	Next file
    End Sub

    So the MessageBox in your case is where you logic goes to perform say a new row in your DataGridView.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 1, 2020 4:42 PM
  • profile for Karen Payne on Stack Exchange

    Hey thanks sooo much,

    It worked just great :)

    If you don't mind, got a minor question:

    In DragDrop Event:

    Dim files = CType(e.Data.GetData(DataFormats.FileDrop), String())

    To be more .NET I use:

    Dim files = Convert.ToString(e.Data.GetData(DataFormats.FileDrop))

    But this does not work, while your CType works! Am I wrong using Convert?

    Friday, May 1, 2020 6:17 PM
  • profile for Karen Payne on Stack Exchange

    Hey thanks sooo much,

    It worked just great :)

    If you don't mind, got a minor question:

    In DragDrop Event:

    Dim files = CType(e.Data.GetData(DataFormats.FileDrop), String())

    To be more .NET I use:

    Dim files = Convert.ToString(e.Data.GetData(DataFormats.FileDrop))

    But this does not work, while your CType works! Am I wrong using Convert?

    Sorry for the delay, mine works because I'm casting to a string array which is what you have, not a string. You can see the different with me doing String() rather than String. 

    Lastly, for kicks C# string array is string[]. I converted my C# to VB e.g.

    var files = (string[])e.Data.GetData(DataFormats.FileDrop);


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, May 1, 2020 9:53 PM
  • Thanks, it was my bad, I had to use:

    DirectCast instead of CType :D

    Thursday, May 7, 2020 4:09 PM