(vb) Tutorial - Adding only images to a listbox

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
5 posts Page 1 of 1
Contributors
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Hey guys,

This is a very simple tutorial on how you can add items to list box based on them only being a png image file, and then preview the images.

Image

You need two controls, a listbox and a picturebox.

Since we're using DirectoryInfo we need to add an import.
Code: Select all
Imports System.IO
We use the below sub statement to populate the listbox. We set the folder path and loop through all the files in the folder. If the file is a png image we remove the png from the file name, and then add it to the listbox.
Code: Select all
Public Sub PopulateImagesList()
        Try
            Dim di As New DirectoryInfo(Application.StartupPath & "\example\")
            Dim fiArr As FileInfo() = di.GetFiles()
            Dim fri As FileInfo
            For Each fri In fiArr
                If fri.Extension = ".png" Then
                    Dim s As String
                    s = fri.Name
                    lstImages.Items.Add(s.Substring(0, s.Length - 4))
                End If
            Next fri
        Catch ex As Exception

        End Try
    End Sub
On the forms load event we add this:
Code: Select all
PopulateImagesList()
Now we need to be able to select an item and preview the image.
Code: Select all
Private Sub lstImages_SelectedValueChanged(sender As Object, e As EventArgs) Handles lstImages.SelectedValueChanged
        Dim img As String = lstImages.SelectedItem.ToString
        Try
            pctrPreview.BackgroundImage = Image.FromFile(Application.StartupPath & "\example\" & img & ".png")
        Catch ex As Exception

        End Try
    End Sub
Because we removed .png from the file name we add it back to the full path of the image and then display it in our picturebox.

View pastebin of source code

Download entire project

If you have any questions leave them below, and if you liked my tutorial remember to +rep :)
Last edited by smashapps on Sat Apr 02, 2016 2:00 pm, edited 1 time in total.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
anthonygz
Member
Member
Posts: 48
Joined: Sun Feb 13, 2011 7:25 pm

A neat use of image generating in vb, basic but it's very well written.

What you can do with this source code:
Vb.net Image Board Community if combined with FTP and uploading.
You can upload the images by simple methods and implement a folder created under the username of the uploader, then for comments you can link the comment box by a textbox under the image's name .txt or whatever. Like for example, make under the image names folder folders named Comment1 or Comment2 or Comment3 by detecting if those exist or not. As for managing each users post, you can delete the images folder via FTP. Then to display them, the user can search for it directly with "tags", or the user his or herself. Easy? No. Possible? Yes, because I've done it before when freelancing forma client. He wanted the users to have the ability to upload, manage, share, comment, and delete images if they owned it; and staff can delete anything they wanted depending on their rank on his forums he offered $3000 to the person who could do this because nobody he worked with has accomplished his requests. So I did, and he was extremely happy about it thanking me for doing this. I received the payment in the mail the same day happy not for the money, but that I did something no one else could.

Or you can make a slideshow, or a 3d slideshow with this code if you got creative or took it far enough working on it.
So many possibilities, so many projects, hope I inspired someone
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

That's awesome #anthonygz it is very possible indeed to write that, but quite some task indeed! Congratulations on winning the freelance job, I would love to be paid for what I love doing! Nice work.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

First of all nice tutorial! second you should really limit the use of the Try Catch methods, they imply that your code is unstable and that you're not sure of the outcome. Plus you should always handle the exceptions yourself and NOT let the system do it for you.
You can find me on Facebook or on Skype mihai_92b
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

That is a good point #TechVB that's something with my code I should brush up on. I'll be more aware of it in the future, thanks.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
5 posts Page 1 of 1
Return to “Tutorials”