Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with loading icons in picturebox 1

Status
Not open for further replies.

assimang

Programmer
Mar 11, 2008
96
Hello everybody,

I have a teaching typing form in my application and when the user completes an exercise if his accuracy and his speed are good I want in a picturebox to display a png icon and if the his speed and accuracy are bad, to display a gif icon. These icons are in resources folder. I am wandering if in vb.net there is something like myapp.path in vb6 to determine the filepath of my application?
I was thinking
EmotionPictureBx.Image = Image.FromFile(My.Resources.smile)
but it gives me error Error Value of type 'System.Drawing.Bitmap' cannot be converted to 'String'. What can I do? I don't want to determine a path like "c:\smile" for example because when I finish the program and deploy it, I don't want my app searches the icon in a hard disk. It is sure that will give error. It's expected that I will install it in other computers, so it must be somewhere in my application like resources maybe, and I have to find a way to access it and load it into the picturebox. Any help much appreciated.

Thanks in advanced.
 
To get the startup directory of your application: Application.StartupPath

I would suggest adding an ImageList to your application and fill your PictureBox from there:

PictureBox1.Image = ImageList1.Images(0)
PictureBox1.Image = ImageList1.Images(1)
etc.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
If it is a resource you don't use a path location to them. Lets say your program is call myProgram. You would then use:

Code:
EmotionPictureBx.Image = Global.myProgram.My.Resources.Resources.smile
You may be able to use just My.Resources.Resources.smile as well, but I'm not sure.

-I hate Microsoft!
-Forever and always forward.
 
Thanks both of you,
I want ask both of you, when you deploy the application do I have any problem with these ways? Does all resources are being deployed?

Thanks again
 
With mine no. With the other as long as you have the installer install the images to the same subfolder then it will be fine as well. Application.StartupPath always gives the path the .exe was ran from.

-I hate Microsoft!
-Forever and always forward.
 
Wait, I said that wrong. When you preload a ImageList it save the image similar to resources. They become a part of the program.

-I hate Microsoft!
-Forever and always forward.
 
Thank you Sorwen, i prefere your way, i think it's nice just for 2 icons. I forgot to ask you what determines global?

Thanks both of you.
 
Generally if I just have a few I use the way I showed you and if I have a lot I use what jebenson showed. As far as I know it is just personal preference and scope. Since anything in the program automatically has access to the program resources.

If you asking what determines when you should use global I couldn't say. As far as I know global is a namespace like any other and can be left or dropped the same way. I tend to use it for two reasons. One, I forgot what all the namespaces I needed to get down to the final namespaces was so I just follow through the autolist members to find it (my memory is getting bad lately). Second, when I've created something else and looked at the design code and it selected it in that manner so that is the way I remember it. Third, when that is the only way it will find the namespace.

That was how I found out about setting an Image from a resource. I had created something (I don't remember what) and when I used a resource image from the properties it set it to the Global.myProgram.My.Resources.Resources.smile (I noticed it while I was in there changing other design code). I tried playing around with it a little, but it didn't like some of the changes. In the end to keep it easier for me to remember I always just type global now.

Global."What did I call this program?"."It is 'My' program"."I need to use Resources"."I have to do it again."."What did I call that picture again?"
Works for me. :)

-I hate Microsoft!
-Forever and always forward.
 
Doh! Didn't mean to click submit yet. One other thing is that a lot of what I learn is self taught through trial and error so it may not always be the best way, but it has worked for me several times so I'll post it. That is not including all the many things the great people that post here have taught me.

-I hate Microsoft!
-Forever and always forward.
 
Thank you so much Sorwen, it works nice for me too. And it save me learninig about imagelist and maybe for more typing code too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top