<p>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?</p>

<p>I tried getting <code>button1.BackgroundImage.ToString()</code> but that yields <code>System.Drawing.Bitmap</code> instead of <code>Resources._1</code> 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).</p>

<p>Thanks for your help.</p>

[{'answer_id': 15071, 'author': 'Landon', 'author_id': 1597, 'author_profile': 'https://Stackoverflow.com/users/1597', 'pm_score': 3, 'selected': True, 'text': "<p>Why don't you just put the images in an array?</p>\n"}, {'answer_id': 15076, 'author': 'Chris Farmer', 'author_id': 404, 'author_profile': 'https://Stackoverflow.com/users/404', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 12660196, 'author': 'zahir', 'author_id': 311618, 'author_profile': 'https://Stackoverflow.com/users/311618', 'pm_score': 0, 'selected': False, 'text': '<pre><code>class YourClass\n{\n    private IEnumerator&lt;Image&gt; enumerator;\n\n    YourClass(IEnumerable&lt;Image&gt; images)\n    {\n        enumerator = (from i in Enumerable.Range(0, int.Max)\n                      from image in images\n                      select image).GetEnumerator();\n        enumerator.MoveNext();\n    }\n\n    public Image CurrentImage { get { return enumerator.Current; } }\n\n    public void OnButtonClick() { enumerator.MoveNext(); }\n}\n</code></pre>\n\n<p>You can use this code as a backing class for your control under the assumption that user wont click the button more than two billion times. </p>\n\n<p>Just note that once this class is created you cannot modify given image list outside. If you want to do such things you need to implement disposable pattern and dispose the enumerator accordingly.</p>\n'}]
['https://Stackoverflow.com/questions/15066', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/271/']