Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
Hi Please kindly pay attention to below code:

VB
Me.btn1.Image = My.Resources.PicOfBTN01
Me.btn2.Image = My.Resources.PicOfBTN02
Me.btn3.Image = My.Resources.PicOfBTN03
 .
 .
 .
Me.btn30.Image = My.Resources.PicOfBTN30


I need to know, how can I do this job, with a FOR ?
Because I don't want to type a line for 30 times :(

Thanks.
Posted

1 solution

It's probably easier to type the 30 lines...

Getting the resource itself isn't too bad:
VB
Dim obj As Bitmap = My.Resources.ResourceManager.GetObject("MyPic2")
PictureBox1.Image = obj

Will do it (though you would probably want to cache the images in an array if you use them often as it returns a new image each time) - so all you have to do is generate the resource name "picOfBtn" + integer value in the loop.

But...then you have to loop through all the controls, identify the buttons you want to set an image for, get its name (Easy: the "Name" property will do that), break the name down to get the integer, build the property name string and then you can use the ResourceManager to get the image.

See what I mean? It's faster to do the lines yourself...
 
Share this answer
 
Comments
Sh.H. 18-Jan-14 8:17am    
Thanks
Would you please kindly write a simple code with regards as your explanation?
OriginalGriff 18-Jan-14 8:19am    
No.
You are trying to save effort typing lines, by getting me to do the work instead?
That doesn't seem very fair to me...
Sh.H. 18-Jan-14 8:27am    
I asked because I didn't get your meaning ... anyway... thanks.
OriginalGriff 18-Jan-14 8:42am    
Which part did you not get the meaning of?
You know how to loop through controls on a form?
You know how to identify which buttons you are interested in (And remember, I don't)?
And so on...
Sh.H. 18-Jan-14 10:37am    
Look, I need to know the code, which I should use, instead of 30 times typing... Would you please do a favor to me and let me know it?
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900