1

I have 1 ListBox with files names, obtained with this code:

Dim files As New List(Of String) files.AddRange(IO.Directory.GetFiles(Xpath, "*.txt"). Select(Function(f) IO.Path.GetFileNameWithoutExtension(f)))

I want to load at PictureBox a list of images with the same initial name.

Example: If i select "house" Item at ListBox, I want to get (for show) all images starting with the same string. In this example the files "houses.jpg", "house 1.jpg", "house 2.jpg" "house 2 backyard.jpg", "house(4).jpg","house roof.jpg", and anyother found (on the same directory) must be shown at picturebox...

How can i list/get the (in part) same named files? to use at PictureBox (I suppose this must filter and exclude the original .txt to avoid errors). The files are into a static folder inside program's path. Its easy to load 1 image of each file with exactly same name, I use:

PictureBox1.image = (Application.StartupPath & "\buildings\" & ListBox1.SelectedItem.ToString & ".jpg")

but my purpouse is detect and use more than 1 image with the same part name when it exist

Maybe the first step is create a kind of list of them... but I don't have idea.

arc95
  • 97
  • 1
  • 8

1 Answers1

1

Having you added strings to the ListBox items collection, you can use Linq to retrieve the items you want pretty easily with

Dim names = ListBox1.Items.
            Cast(Of String)().
            Where(Function(x) x.StartsWith("house")).ToList()

Now you have the items that starts with the proposed constand in the names list. Of course a picturebox shows only one image at time, so it is up to you to iterate over this list (perhaps using a couple of buttons to move forward or backward in the names list) and then show the images in your picturebox one after the other.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • In the same way you add the files names without extension to your files listbox. _names_ is just a list of strings so _nameofyourotherlistbox.AddRange(names)_ If you need to pass this list between forms then you should simply add a public property to the receiving form of type List(Of String). Sorry but busy at work now. – Steve Feb 08 '18 at 08:10
  • `Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged Dim names = ListBox4.Items. Cast(Of String)(). Where(Function(x) x.StartsWith(ListBox2.SelectedItem.ToString)).ToList() ListBox5.Items.AddRange(names)` Yes but, I get error at "addrange", it says: value of type list(of string) cannot be converted to Listbox.objectcollection – arc95 Feb 08 '18 at 19:41
  • Solved!! I change: `x.StartsWith(ListBox2.SelectedItem.ToString)).ToList()` into `x.StartsWith(ListBox2.SelectedItem.ToString)).ToArray() ` Now works!! Great! – arc95 Feb 08 '18 at 20:17