开发者

getting back default button properties C# - Windows forms

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开发者_StackOverflow中文版.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();


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);
...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜