changing panel image in runtime

Pozi

New member
Joined
Oct 20, 2006
Messages
2
Programming Experience
Beginner
Yes I am a newb.

I am taking a into to VB 2005 class. Our latest project involves changing the a panel's background image with a button.

I can get the following code to work but only when I have the picture hard pathed to the directory. In runtime it does not find the image when the filename or any combination short of the entire path is listed.

panGraphics.BackgroundImage = Image.FromFile("E:\blah\blah\blah\bin\Debug\Graphics\Picture1.jpg")

panGraphics.BackgroundImage = Image.FromFile("\Picture1.jpg") 'DOES NOT WORK

Then the second part of my question is I want to use this btnChange as a click even to also change the text of a label. But when I try to setup an If statement it will not work.

If panGraphics.BackgroundImage = Image.FromFile(":/Picture1.jpg") Then
panGraphics.BackgroundImage = Image.FromFile("\Picture2.jpg")
lblInformation.text = ("#2")
ElseIf panGraphics.BackgroundImage = Image.FromFile(":/HauntedHouse.jpg") Then
panGraphics.BackgroundImage = Image.FromFile("\HauntedHouse.jpg")
lblInformation.text = ("#1")
End If

I know I am totally off but this is what I got right now and hopefully I can get steered in the right direction.
 
Ok, having just posted Ive realised what an old post this is :/ apologies for bumping it. I guess you never know, it might help someone *-)

You must always list the entire directory path, it needs to know exactly where to look. It's kind of like giving the Country and street name for someones address- you have no idea what Town/ City the street is in, and there is a chance there is more than one.

If you have multiple images from the same directory you could have a string variable holding the path and concatenate it with the file name:

VB.NET:
Dim path as String = "C:\Documents and Settings\User\My Documents"

panGraphics.BackgroundImage = Image.FromFile(path & "\picture1.jpg")

Also, it might be useful to declare the images as variables if you will keep using them, and it will make the if statement simpler:

Dim pic1 as image = Image.FromFile(path & "\picture1.jpg")
Dim pic2 as image = Image.FromFile(path & "\picture2.jpg")

VB.NET:
If panGraphics.BackgroundImage = pic1 Then
panGraphics.BackgroundImage = pic2
lblInformation.text = "#2"
Else
panGraphics.BackgroundImage = pic1
lblInformation.text = "#1"
End If

Hope that helps :)

PS {code not checked, possible typos}
 
Ok, having just posted Ive realised what an old post this is
A year or so it's very old :p
You must always list the entire directory path, it needs to know exactly where to look.
Relative paths are just about always valid, but you might never know exactly where current directory is or when it may be changed by any external process. So using absolute paths are a must, there exist helpers in the form of defined paths, for example Application.StartupPath is very common to use as base path. (btw, in such cases it is also not uncommon to include files as local resources instead of using filesystem)
 
Back
Top