Page 1 of 1

Drag and drop

Posted: Fri Apr 30, 2010 4:27 pm
by DarkyKnife
Hello, i wanted to know how to drag and drop an item and listbox shows dropped file name(not the path, only name). Thanks.

Re: Drag and drop

Posted: Fri Apr 30, 2010 8:08 pm
by CodenStuff
Hello DarkyKnife,

First off welcome to the site cooll;

This code will allow you to drag-drop items into the listbox and show only the filename of each file. To make this code work you first need to select your "ListBox" control and change its "AllowDrop" propertie to 'True' and then add the following code.
Code: Select all
Private Sub ListBox1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub

    Private Sub ListBox1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim draggedFiles As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
            For Each filename As String In draggedFiles
                ListBox1.Items.Add(FileIO.FileSystem.GetFileInfo(filename).Name)
            Next
        End If
    End Sub
Hope that helps cooll;

Re: Drag and drop

Posted: Fri Apr 30, 2010 9:52 pm
by Lewis
Hello CodenStuff,
I know this code was not sumbited for me but it did help alot !! Thanks,
Lewis

Re: Drag and drop

Posted: Sat May 01, 2010 9:37 am
by DarkyKnife
Thank you very much, this helped me.
P.S. Very nice and usefull site with friendly people :)