Results 1 to 8 of 8

Thread: VS2012 - How do I change PictureBox when a ListBox value is selected?

  1. #1
    New Member
    Join Date
    Sep 12
    Posts
    3

    VS2012 - How do I change PictureBox when a ListBox value is selected?

    Hey guys,
    I'm new to this forum because I've seen some really good guides on here. I came across this guide yesterday, and have tried to implement it with my current project, but it's not working, so I thought I'd might as well ask for help and see if you could help me out.

    Basically, I have a PictureBox and a ListBox with a lot of values. I would like to make it so that whenever you select a new value in the ListBox, it will change the PictureBox image. This means that every ListBox item should have a image linked to it, so whenever you select it, it loads on the PictureBox area.

    How would I go about doing this?

    Any help is appreciated! I just started learning VB.NET so if you could try and describe in-depth, that'd be amazing.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,860

    Re: VS2012 - How do I change PictureBox when a ListBox value is selected?

    You need to create some sort of relationship between the items in the ListBox and the Images you want to display. There are various ways you can do that but the simplest would be to put the Images in an array. You can handle the SelectedIndexChanged event of the ListBox to be notified when the user selects an item. You can get the index of that item using the SelectedIndex property and use that as an index into the array to get the corresponding Image. You can then assign that Image to the Image property of the PictureBox.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,860

    Re: VS2012 - How do I change PictureBox when a ListBox value is selected?

    By the way, if you've already tried something and it didn't work then you did it wrong. We don't know what you did so we can't possibly tell you what you did wrong. If you have code then post it. If it didn't work then tell us what happened. Then we can help you fix it.

  4. #4
    New Member
    Join Date
    Sep 12
    Posts
    3

    Re: VS2012 - How do I change PictureBox when a ListBox value is selected?

    Quote Originally Posted by jmcilhinney View Post
    By the way, if you've already tried something and it didn't work then you did it wrong. We don't know what you did so we can't possibly tell you what you did wrong. If you have code then post it. If it didn't work then tell us what happened. Then we can help you fix it.
    First of all, thanks for your replies.

    To be honest, I haven't written any lines of code yet because I don't know which method to use nor how to do it.
    Anyways, here's the code that is there right now:
    Code:
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    
        End Sub
    End Class
    I have all the images I need in the Resources area, I just need to link each ListBox item with each resource image, and make them load into the PictureBox once selected. I don't know how to do this, though.

    And once again, thanks for your help.

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,546

    Re: VS2012 - How do I change PictureBox when a ListBox value is selected?

    So are you intending to post a new question for everything you need to learn then because, believe me, people's patience can be very thin with that sort of dependency? There are hundreds, if not thousands, of tutorials available on the net and plenty of good old fashioned books available on the subject too. Nothing in this intended project is so difficult that it won't be covered within the first chapter or so of any of them. We can't do the learning for you and we won't simply hand you code on a plate.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,860

    Re: VS2012 - How do I change PictureBox when a ListBox value is selected?

    I explained how to do it so work through that explanation step by step. Step 1:
    There are various ways you can do that but the simplest would be to put the Images in an array.
    Do you know how to create an array?

  7. #7
    New Member
    Join Date
    Sep 12
    Posts
    3

    Re: VS2012 - How do I change PictureBox when a ListBox value is selected?

    Quote Originally Posted by dunfiddlin View Post
    So are you intending to post a new question for everything you need to learn then because, believe me, people's patience can be very thin with that sort of dependency? There are hundreds, if not thousands, of tutorials available on the net and plenty of good old fashioned books available on the subject too. Nothing in this intended project is so difficult that it won't be covered within the first chapter or so of any of them. We can't do the learning for you and we won't simply hand you code on a plate.
    I'm asking for help, nothing else. You make it sound like it's the end of the world, while it's not. The forums are here to get guidelines if you're stuck. I'm stuck, am I not? And this is my first thread, asking for the only thing I've got a problem with so far. Instead of trying to be rude, feel free to get off your high horse and help around, or just don't post at all.

    Quote Originally Posted by jmcilhinney View Post
    I explained how to do it so work through that explanation step by step. Step 1o you know how to create an array?
    Indeed, I'll try that and see if it works. Cheers!

  8. #8
    Member
    Join Date
    Jul 07
    Posts
    50

    Re: VS2012 - How do I change PictureBox when a ListBox value is selected?

    Maybe you want this?

    Code:
    Public Class Form1
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            MsgBox(ListBox1.SelectedIndex)
            PictureBox1.ImageLocation = ListBox1.SelectedItem
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\")
                ListBox1.Items.Add(foundFile)
            Next
        End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •