Question About listbox.

thompse3

Member
Joined
Jul 13, 2009
Messages
6
Programming Experience
Beginner
What i am having trouble with is,
1. I have a folder full of songs
2. I want all of there extensions in a listbox

Is it possible to drag and drop them into a list box and give the result i want?
If so, how would you do that..

Thank you for the help.
 
You can't just "drag" the list into a listbox. You would need to capture the list of files (using DirectoryInfo and FileInfo). Then you could load the listbox from that.

You can use this for capturing the list...
VB.NET:
Dim di As New DirectoryInfo(foldDialog.SelectedPath.ToString())
Dim fi As FileInfo() = di.GetFiles()
Dim temp As FileInfo

For Each temp In fi
   'load into listbox here
Next
 
Is it possible to drag and drop them into a list box and give the result i want?
Sure it's possible, set up a basic Drag-drop for the listbox and handle the DataFormats.FileDrop.
Walkthrough: Performing a Drag-and-Drop Operation in Windows Forms (see Dropping Data)
When you have the file/folder list use the tools in System.IO Namespace () to determine if they are files or folders (ex File.Exist) and get the extension (Path.GetExtension). Add extension to Listbox.Items if it don't Contain it already.

Code samples, for DragEnter handler:
VB.NET:
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    e.Effect = DragDropEffects.Copy
End If
for DragDrop handler:
VB.NET:
For Each path As String In CType(e.Data.GetData(DataFormats.FileDrop), String())
    If IO.File.Exists(path) Then
        AddExtension(path)
    Else
        For Each file As String In IO.Directory.GetFiles(path)
            AddExtension(file)
        Next
    End If
Next
AddExtension method:
VB.NET:
Private Sub AddExtension(ByVal file As String)
    Dim ext As String = IO.Path.GetExtension(file).ToLower
    If Not ListBox1.Items.Contains(ext) Then
        ListBox1.Items.Add(ext)
    End If
End Sub
 
Woah this is more complex then i thought it would be, this is not at my level, I just wanted to add that cool feature to my program.
but if you could still help that would be great.

This is what i have, not working.
VB.NET:
Private Sub ListBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragDrop
        For Each path As String In CType(e.Data.GetData(DataFormats.FileDrop), String())
            If IO.File.Exists(path) Then

            Else
                For Each file As String In IO.Directory.GetFiles(path)
                    ListBox2.Items.Add(file)
                Next
            End If
        Next
    End Sub

    Private Sub ListBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub

    Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged

    End Sub
    Private Sub AddExtension(ByVal file As String)
        Dim ext As String = IO.Path.GetExtension(file).ToLower
        If Not ListBox1.Items.Contains(ext) Then
            ListBox1.Items.Add(ext)
        End If
    End Sub
Thats what you gave me
 
I guess you didn't read the drag-drop article and didn't set AllowDrop to True.
 
thompse3 said:
If IO.File.Exists(path) Then

Else
For Each file As String In IO.Directory.GetFiles(path)
ListBox2.Items.Add(file)
Next
End If
On closer look at the code I see it is different from what I posted, most notably you do nothing if the one of the paths droped is a file. Why not try the code I posted? Also try to READ the code and try to understand it. Take into account that you can drag a selection of files and folders or both in one operation. The sample code handles all those cases.
 
Back
Top