tags:

views:

2182

answers:

3

I have a form in C# that has a button that, when clicked, I want the background image to cycle through a set of images (which I have as resources to the project). The images are named '1', '2', etc. and each time I click the button I want its background image to increment to the next one and go back to "_1" when it gets to the highest. Is there a way to do this?

I tried getting button1.BackgroundImage.ToString() but that yields System.Drawing.Bitmap instead of Resources._1 like I was thinking it would (in which case I could just get the last character and switch on that to change the background to the appropriate new image).

Thanks for your help.

+3  A: 

Why don't you just put the images in an array?

Landon
A: 

You could subclass Button and override the BackgroundImage property so you can better keep track of the current resource that represents the image. You might also override the onclick method to internally handle cycling to the next image, though that might be a little weird if the resources are handled outside of your derived button class.

Chris Farmer
A: 

Putting the images in an array was really easy and should have been horribly obvious.

Thanks for the easy tip Landon!

Justin Bennett