Support Forums

Full Version: [VB.NET] - ListBox Drag & Drop - [TUT]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Not really a tutorial, more just a simple guide.
People love programs which are simple and easy to use, which is why the whole drag & drop feature is so big lately.

The following snippets of code will allow users to drag files from their computer, directly into your ListBox control, like so...

[Image: dragdrlpl.gif]

Firstly, under your ListBox properties, set AllowDrop to True.
Under your ListBox's DragOver event, add the following:

Code:
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
End If

The above code simply determines what is happening when a file is dragged over the ListBox control.

Next up, add the following to the ListBox's DragDrop event:

Code:
Dim file As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop, True), System.String())

        For Each s As String In file
            If s.Contains(".mp3") Then
                ListBox.Items.Add(s)
            Else
                Exit Sub
            End If
        Next

This gets the data of the file(s) and adds the location as an Item into your ListBox. I've also set a restriction on it, so that only .mp3 files can be added, although this can be removed/edited to fit your needs.

Hope this helps some of you.
Thumbsup
If you wanted to be fancy you could do this with sorting the parts of the filepath string out to get the file format, filename, and directory location into 3 different segments, or use a listbox with 3 columns for those items on every dragdrop
Nice detailed and helpful tutorial. I hope to see more like this. Thanks for the share.
This is nice it could be helpful for beginners, Good Job Smile
Thanks for this tutorial, i will use this for something i am making.
Thanks for the share this is actually very useful.
Thanks For This Guide.
I Never Seen This Trick Anywhere Else.
I've got one that will drag and drop files with multiple columns to a listview if anyone wants that, i'll post it up at some point.
Cool man I would like to take a peak at that thanks again.
I don't get what the point is of this... Sorry!
Pages: 1 2