שתף באמצעות


Show images in a PictureBox with ListBox

Question

Friday, June 3, 2016 10:30 AM

Well I have a ListBox with some names of files in my computer, and I want to show that files (images) in a PictureBox.

Someone knows how can I do that? My code looks like this:

For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\Users\Utilizador\Desktop\, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.dll")
            PictureBox1.Image = Image.FromFile(foundFile)
        Next

Thank you!

All replies (8)

Friday, June 3, 2016 10:39 AM ✅Answered

Did you really mean *.dll or did you mean something like *.jpg

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator


Friday, June 3, 2016 10:52 AM ✅Answered

Try this

Public Class Form1
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
        If ListBox1.DataSource IsNot Nothing Then
            PictureBox1.Image = Image.FromFile(CType(ListBox1.SelectedItem, Item).FullName)
        End If
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim files =
            (
                From file In My.Computer.FileSystem.GetFiles("C:\Users\Utilizador\Desktop\",
                    FileIO.SearchOption.SearchAllSubDirectories, "*.jpg")
                Select New Item With {.FileName = IO.Path.GetFileName(file), .FullName = file}
            ).ToList

        ListBox1.DisplayMember = "FileName"
        ListBox1.DataSource = files
        ListBox1.SelectedIndex = 0

        AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged

    End Sub
End Class

Friend Class Item
    Public Property FileName As String
    Public Property FullName As String
End Class

Another sample, get multiple file types e.g. png and jpg

Public Class Form1
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
        If ListBox1.DataSource IsNot Nothing Then
            PictureBox1.Image = Image.FromFile(CType(ListBox1.SelectedItem, Item).FullName)
        End If
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim files =
            (
                From file In GetFiles("C:\Users\Utilizador\Desktop\", "*.jpg|*.png")
                Select New Item With {.FileName = IO.Path.GetFileName(file), .FullName = file}
            ).ToList

        ListBox1.DisplayMember = "FileName"
        ListBox1.DataSource = files
        ListBox1.SelectedIndex = 0

        AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged

    End Sub
    Private Function GetFiles(ByVal sourceFolder As String, ByVal filters As String) As List(Of String)
        Return filters.Split("|"c) _
            .SelectMany(Function(filter) IO.Directory _
            .GetFiles(sourceFolder, filter, IO.SearchOption.AllDirectories)).ToList
    End Function
End Class

Friend Class Item
    Public Property FileName As String
    Public Property FullName As String
End Class

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator


Friday, June 3, 2016 12:44 PM ✅Answered

Are you looking to have something looking like this?:


Friday, June 3, 2016 12:58 PM ✅Answered

Hello again,

First off you still have *.dll. Next up, in regards to the last for-each, you can expect one PictureBox to show each image at one time. To show all at one time best to use a FlowLayoutPanel. Then show each image at a small size, subscribe to the click event and do something e.g. show the image in a dialog form or show the image in another picture box.

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim FilePath As String = ""

        ' so I can test on my computer
        If IO.Directory.Exists("C:\Users\Utilizador\Desktop") Then
            FilePath = "C:\Users\Utilizador\Desktop"
        Else
            FilePath = "C:\Users\Karens\Pictures"
        End If

        Dim files As List(Of Item) =
            (
                From file In GetFiles($"{FilePath}\", "*.jpg", IO.SearchOption.AllDirectories)
                Select New Item With {.FileName = file, .FullName = IO.Path.GetDirectoryName(file)}
            ).ToList

        ListBox1.DisplayMember = "FileName"
        ListBox1.DataSource = files
        ListBox1.SelectedIndex = 0
    End Sub
    Private Function GetFiles(ByVal sourceFolder As String, ByVal filters As String, ByVal options As IO.SearchOption) As List(Of String)
        Return filters.Split("|"c) _
            .SelectMany(Function(filter) IO.Directory _
            .GetFiles(sourceFolder, filter, options)).ToList
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If ListBox1.DataSource IsNot Nothing Then

            FlowLayoutPanel1.Controls.Clear()

            Dim files As List(Of String) =
                GetFiles($"{CType(ListBox1.SelectedItem, Item).FullName}\", "*.jpg", IO.SearchOption.TopDirectoryOnly)

            For Each file As String In files

                Dim p As New PictureBox With {.Parent = FlowLayoutPanel1, .Image = Image.FromFile(file), .ImageLocation = file}

                AddHandler p.Click, Sub(s As System.Object, a As System.EventArgs)
                                        Dim pictBox As PictureBox = CType(s, PictureBox)
                                        MessageBox.Show($"do something with {Environment.NewLine}{pictBox.ImageLocation}")
                                    End Sub

            Next
        End If
    End Sub
End Class
Friend Class Item
    Public Property FileName As String
    Public Property FullName As String
End Class

Yellow area is our flow layout panel

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator


Friday, June 3, 2016 10:44 AM

Yes you're right, but now it show only one image, the others don't work!

Thank you for your answer


Friday, June 3, 2016 11:13 AM

Well, your code works but not the way that I wanted to...

In my program the user need to put the name of a folder, and in the ListBox appears the name of the files (only images) that are in that one folder. But when I click on the files it only shows one image, and actually don't appear the name of the file, just the way!

My code is this:

     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

  For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\Users\Utilizador\Desktop\, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.dll")
            ListBox1.Items.Add(foundFile)
        Next

    End Sub

rivate Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\Users\Utilizador\Desktop\, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.jpg")
            PictureBox1.Image = Image.FromFile(foundFile)
            PictureBox1.SizeMode = GetAutoSizeMode()
        Next

    End Sub

Thank you


Friday, June 3, 2016 1:27 PM | 2 votes

This is what I need to hapen: When I click on the Item in the ListBox and in "Ver imagem" the file (image) appear, but it just shows the same pic all the time

What needs to happen now is you should study what Karen showed you and make a attempt to understand it and do things yourself.


Friday, June 3, 2016 2:40 PM

Are you looking to have something looking like this?:

What this is is the OpenFileDialog control with the default miniature.

It's as simple as adding an OpenfileDialog control to your Form and call OpenfileDialog.Show...