Dim rainfall = rainfall.jpg Dim cheetah = cheetah.jpg Dim jester = jester.jpg Dim tangerine = tangerine.jpg Dim ragdoll = ragdoll.jpg If cboSongs.SelectedItem = 1 Then picSongs = rainfall ElseIf cboSongs.SelectedItem = 2 Then picSongs = cheetah ElseIf cboSongs.SelectedItem = 3 Then picSongs = jester ElseIf cboSongs.SelectedItem = 4 Then picSongs = tangerine ElseIf cboSongs.SelectedItem = 5 Then picSongs = ragdoll End If
Help with VB click event changes picture in picturebox
#1
Help with VB click event changes picture in picturebox
Posted 23 March 2008 - 05:47 PM
hi - i'm Hannah and this is my first post. basically here's my issue - I'm trying to write a program in VB that will let the user choose a song from a combobox and then click a button and the form will then play the song chosen and display an image related to the song. I have a default image to begin with and I'm trying to figure out how to make the default change to a different picture for each song. Here's what I have so far, and it's giving me an error, I may be way off. Any help is greatly appreciated!
#2
Re: Help with VB click event changes picture in picturebox
Posted 23 March 2008 - 09:15 PM
Well you are getting errors for a couple reasons. The first is that when setting the values of your variables you don't have quotes around the image names
Second errors are occurring because you're trying to set the actual picture box to the variable, not the Picture property
To make this work, take a look at the modifications I made to your code below. Here I use the LoadPicture function in Visual Basic 6 to take the string value set in the variables and convert it to an image object. I set the Picture property of the PictureBox to that value.
Also, I changed your code to use a Select Case Method rather than the number of If..ElseIf statements you had, this not only makes it easier to read but is more maintainable in my opinion.
Now I am making some assumptions here
Also, you may want to ensure that your images are available dynamically, or that they are in the same directory as your executable, otherwise the path you provided will not work.
Dim rainfall = rainfall.jpg Dim cheetah = cheetah.jpg Dim jester = jester.jpg Dim tangerine = tangerine.jpg Dim ragdoll = ragdoll.jpg
Second errors are occurring because you're trying to set the actual picture box to the variable, not the Picture property
If cboSongs.SelectedItem = 1 Then picSongs = rainfall ElseIf cboSongs.SelectedItem = 2 Then picSongs = cheetah ElseIf cboSongs.SelectedItem = 3 Then picSongs = jester ElseIf cboSongs.SelectedItem = 4 Then picSongs = tangerine ElseIf cboSongs.SelectedItem = 5 Then picSongs = ragdoll End If
To make this work, take a look at the modifications I made to your code below. Here I use the LoadPicture function in Visual Basic 6 to take the string value set in the variables and convert it to an image object. I set the Picture property of the PictureBox to that value.
Also, I changed your code to use a Select Case Method rather than the number of If..ElseIf statements you had, this not only makes it easier to read but is more maintainable in my opinion.
'Need to put the image names in quotes since they
'are actually string variables
Dim rainfall = "rainfall.jpg"
Dim cheetah = "cheetah.jpg"
Dim jester = "jester.jpg"
Dim tangerine = "tangerine.jpg"
Dim ragdoll = "ragdoll.jpg"
'Here, instead of a bunch of If..ElseIf statements we will
'use a Select Case statement
Select Case cboSongs.SelectedItem
Case 1
picSongs.Picture = LoadPicture(rainfall)
Case 2
picSongs.Picture = LoadPicture(cheetah)
Case 3
picSongs.Picture = LoadPicture(jester)
Case 4
picSongs.Picture = LoadPicture(tangerine)
Case 5
picSongs.Picture = LoadPicture(ragdoll)
End Select
Now I am making some assumptions here
- picSongs is the name of your PictureBox
- the displayed values of your ComboBox are 1,2,3,4,5. If they're not you may want to use the SelectedIndex property of the ComboBox
Also, you may want to ensure that your images are available dynamically, or that they are in the same directory as your executable, otherwise the path you provided will not work.
#3
Re: Help with VB click event changes picture in picturebox
Posted 24 March 2008 - 11:21 AM
Thanks, I changed my code but I'm still getting an error in the Select Case statement: 'Picture' is not a member of 'System.Windows.Forms.PictureBox' and that 'LoadPicture' is not declared. Is there something else I need to import or something? Also about the assumptions, you were right and I put all of the picture files in the resource folder.
#4
Re: Help with VB click event changes picture in picturebox
Posted 25 March 2008 - 07:30 AM
Well now I know your problem, you're using VB.Net not VB6 or older. Moved to the VB.Net Forum
Since this is VB.Net and not VB6 things need to change a bit. Below are the modifications needed to accomplish this in VB.Net. LoadImage isnt a VB.Net function, rather it's a VB6 function.
For VB.Net we'll use the Image.FromFile Method, which is a member of the System.Drawing Namespace.
Also, the VB.Net PictureBox doesnt have a Picture property, rather is has a Image Property:
Since this is VB.Net and not VB6 things need to change a bit. Below are the modifications needed to accomplish this in VB.Net. LoadImage isnt a VB.Net function, rather it's a VB6 function.
For VB.Net we'll use the Image.FromFile Method, which is a member of the System.Drawing Namespace.
Also, the VB.Net PictureBox doesnt have a Picture property, rather is has a Image Property:
'Need to put the image names in quotes since they
'are actually string variables
Dim rainfall As String = "rainfall.jpg"
Dim cheetah As String = "cheetah.jpg"
Dim jester As String = "jester.jpg"
Dim tangerine As String = "tangerine.jpg"
Dim ragdoll As String = "ragdoll.jpg"
'Here, instead of a bunch of If..ElseIf statements we will
'use a Select Case statement
Select Case cboSongs.SelectedItem
Case 1
picSongs.Image = New System.Drawing.Image.FromFile(rainfall)
Case 2
picSongs.Image = New System.Drawing.Image.FromFile(cheetah)
Case 3
picSongs.Image = New System.Drawing.Image.FromFile(jester)
Case 4
picSongs.Image = New System.Drawing.Image.FromFile(tangerine)
Case 5
picSongs.Image = New System.Drawing.Image.FromFile(ragdoll)
End Select
This post has been edited by PsychoCoder: 25 March 2008 - 07:37 AM
#6
Re: Help with VB click event changes picture in picturebox
Posted 26 March 2008 - 08:07 AM
Add a reference to the System.Drawing Namespace:
Next add this line to the top of your class
That should take care of that error for you
- Click Project in the menu bar
- Select Add Reference
- In the .Net tab scroll down and place a check in the CheckBox next to System.Drawing
- Click OK
Next add this line to the top of your class
Imports System.Drawing
That should take care of that error for you
#9
Re: Help with VB click event changes picture in picturebox
Posted 28 March 2008 - 08:22 AM
ya...heres my code
Imports System.Drawing Public Class Tutorial Private Sub mainGame_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown Dim leftGuy As String = "leftGuy.JPG" Dim rightGuy As String = "rightGuy.JPG" Dim forwardGuy As String = "forwardGuy.JPG" Dim backwardGuy As String = "backwardsGuy.JPG" Select Case e.KeyCode Case Keys.Left picPlayer.Image = New System.Drawing.Image.FromFile(leftGuy) Case Keys.Right picPlayer.Image = New System.Drawing.Image.FromFile(rightGuy) Case Keys.Up picPlayer.Image = New System.Drawing.Image.FromFile(backwardsGuy) Case Keys.Down picPlayer.Image = New System.Drawing.Image.FromFile(forwardGuy) End Select End Sub End Class
This post has been edited by yowutup11: 28 March 2008 - 08:25 AM
#10
Re: Help with VB click event changes picture in picturebox
Posted 28 March 2008 - 05:02 PM
so i made a complete new form...went through all the steps...and when I came to checking the box next to System.Drawing there was no box...I am using Visual Studio 2005...do I just double click it or wat do I need to do?
#14
Re: Help with VB click event changes picture in picturebox
Posted 03 April 2008 - 06:15 AM
I had the same problem. I highlighted and clicked okay but it still shows the error, I don't have access to my file right now but I am using the Academic version of Visual Studio.
This post has been edited by {SNLover87}: 03 April 2008 - 06:21 AM

Reply





MultiQuote




|