BP Forums
Files in a listbox - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+---- Thread: Files in a listbox (/showthread.php?tid=464)



Files in a listbox - Hurricane - 02-26-2012

I would first like to say thanks for putting up a website like this! I have looked through and watched your tutorial on having files come up in a listbox. I'm still having problems getting them to show up though....I'm a newbie to all of this. This is the code I'm using below, but when I run the project I don't see any files in the listbox.....thanks for the help in advance!!


Public Class Form3

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim directory = "C:\PIF"
Dim files() As System.IO.FileInfo
Dim dirinfo As New System.IO.DirectoryInfo(directory)
files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
For Each file In files
ListBox1.Items.Add(file)
Next
End Sub
End Class


Re: Files in a listbox - brandonio21 - 02-26-2012

Well, this doesn't work because your code is inside of the SelectedIndexChanged method. This means that you would have to essentially change what is selected in the listbox in order to get the files to appear. However, you cannot change your selection because there is nothing to select!

Try moving your code around like this:
Code:
Public Class Form3

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim directory = "C:\PIF"
Dim files() As System.IO.FileInfo
Dim dirinfo As New System.IO.DirectoryInfo(directory)
files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
For Each file In files
ListBox1.Items.Add(file)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
End Class



Re: Files in a listbox - Hurricane - 02-26-2012

Thanks, appreciate this!!


Re: Files in a listbox - Hurricane - 02-26-2012

One more thing....is there a way that I can make it to where you double click on a file in the listbox and it open?


Re: Files in a listbox - brandonio21 - 02-27-2012

Yeah, try something like this!

Code:
public sub DBL handles ListBox1.MouseDoubleClick
Shell(Listbox1.selecteditem.tostring)
End Sub