VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • Reply Reply

Help with VB click event changes picture in picturebox Rate Topic: -----

#1 {SNLover87}  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 08


Dream Kudos: 0

Share |

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! :D


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



Was This Post Helpful? 0
  • +
  • -


#2 PsychoCoder  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

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

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.
Was This Post Helpful? 0
  • +
  • -

#3 {SNLover87}  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 08


Dream Kudos: 0

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.
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

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:


'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

Was This Post Helpful? 0
  • +
  • -

#5 {SNLover87}  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 08


Dream Kudos: 0

Re: Help with VB click event changes picture in picturebox

Posted 26 March 2008 - 07:46 AM

Oh ok. . .I fixed it and it's giving me an error under each "System.Drawing.Image.FromFile" saying it is not defined?
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

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:

  • 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 :)
Was This Post Helpful? 0
  • +
  • -

#7 yowutup11  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 07-March 08


Dream Kudos: 0

Re: Help with VB click event changes picture in picturebox

Posted 27 March 2008 - 11:14 AM

I tried that same code and I still get the error that it is not defined...?
Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • Icon

Reputation: 345
  • View blog
  • Posts: 9,235
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Re: Help with VB click event changes picture in picturebox

Posted 27 March 2008 - 12:06 PM

Did you add the import statement as PsychoCoder instructed you too?
Was This Post Helpful? 0
  • +
  • -

#9 yowutup11  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 07-March 08


Dream Kudos: 0

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

Was This Post Helpful? 0
  • +
  • -

#10 yowutup11  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 07-March 08


Dream Kudos: 0

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?
Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Re: Help with VB click event changes picture in picturebox

Posted 28 March 2008 - 05:12 PM

Just highlight it and click OK
Was This Post Helpful? 0
  • +
  • -

#12 yowutup11  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 07-March 08


Dream Kudos: 0

Re: Help with VB click event changes picture in picturebox

Posted 28 March 2008 - 05:13 PM

ok...i did that

and the errors are still there...
Was This Post Helpful? 0
  • +
  • -

#13 Jayman  Icon User is offline

  • Student of Life
  • Icon

Reputation: 345
  • View blog
  • Posts: 9,235
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Re: Help with VB click event changes picture in picturebox

Posted 28 March 2008 - 08:03 PM

Zip up your project and upload it as an attachment to your post.

Are you using the Express version of VS?
Was This Post Helpful? 0
  • +
  • -

#14 {SNLover87}  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 08


Dream Kudos: 0

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

Was This Post Helpful? 0
  • +
  • -

#15 Jayman  Icon User is offline

  • Student of Life
  • Icon

Reputation: 345
  • View blog
  • Posts: 9,235
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Re: Help with VB click event changes picture in picturebox

Posted 03 April 2008 - 07:50 AM

Well when you get a chance I would like to look at your entire project. This way I can see what exactly is going on.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • Reply Reply


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users