tags:

views:

38

answers:

2

In my Form1 I am trying to change around 50 button images as follows:

                button1.Image = new Bitmap("C:\\x.jpg");
                button2.Image = new Bitmap("C:\\y.jpg");
                button3.Image = new Bitmap("C:\\z.jpg");
              ..... 

etc....

on another event I want all the 50 buttons to have their default images I set using the designer properties window. Is that possible or should I just declare the images again???

what I have tried and DIDN'T WORK:

tried both:

Properties.Settings.Default.Reset();
Properties.Settings.Default.Reload();
+1  A: 

You will need to manually set in the image again:

foreach (Button b in buttons)
    b.Image = _defaultImage;

However you can make a little method that does this and pass in an array of your buttons. I would make a local form array of all the buttons for easy access.

Adam
Thanks, that saves a lot of searching!
Saher
It would be cool if the form held initial state, but unfortunately, the designer is nothing but a code generator - once the code runs, that's it. You cannot call that code again (`InitializeComponents`) either because it creates objects etc (well you could but it would obliterate your entire form, instead of just the buttons).
Adam
@Adam, so why didn't Reset and Reload methods work?
serhio
Never encountered the methods before, but the Settings container has nothing to do with a form's initial state, so that's why it didn't do what you expected. Don't forget to mark an answer as accepted. The form may have it's own resources, as has been demonstrated in another answer, but they aren't the Properties.Settings stuff.
Adam
actually Reset and Reload do not work.
Saher
+2  A: 

If you don't cache original properties, then you need to reload them from resources:

var resources = 
    new System.ComponentModel.ComponentResourceManager(typeof(Form1));
button1.Image = (Image)resources.GetObject("button1.Image");
button2.Image = (Image)resources.GetObject(button2.Name + ".Image");
...

Or, if you want to reload all component properties:

var resources =
    new System.ComponentModel.ComponentResourceManager(typeof(Form1));
resources.ApplyResources(button1, "button1");
resources.ApplyResources(button2, button2.Name);
...
arbiter
+1 cool, never seen it done that way before. I'll remember that :)
Adam