Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Files in a listbox
#1
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
#2
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
My Blog | My Setup | My Videos | Have a wonderful day.
#3
Thanks, appreciate this!!
#4
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?
#5
Yeah, try something like this!

Code:
public sub DBL handles ListBox1.MouseDoubleClick
Shell(Listbox1.selecteditem.tostring)
End Sub
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  Listbox items text and text boxes? Derek275 3 16,126 11-21-2012, 12:04 PM
Last Post: brandonio21
  How to search multiple files for one string ryan 1 8,803 06-18-2012, 06:04 PM
Last Post: brandonio21
  How to delete all files in a specific folder using VB ? Himansh 4 16,496 03-13-2012, 02:55 PM
Last Post: brandonio21
  Get Files from the C drive Protection4Real 3 13,343 02-10-2011, 07:59 AM
Last Post: Protection4Real

Forum Jump:


Users browsing this thread: 1 Guest(s)