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.
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.
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.
Re: VS2012 - How do I change PictureBox when a ListBox value is selected?
Quote:
Originally Posted by
jmcilhinney
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.
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.
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:
Quote:
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?
Re: VS2012 - How do I change PictureBox when a ListBox value is selected?
Quote:
Originally Posted by
dunfiddlin
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
I explained how to do it so work through that explanation step by step. Step 1:Do you know how to create an array?
Indeed, I'll try that and see if it works. Cheers!
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