代码之家  ›  专栏  ›  技术社区  ›  Saher Ahwal

返回默认按钮属性C#-Windows窗体

  •  1
  • Saher Ahwal  · 技术社区  · 14 年前

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

    等。。。。

    在另一个事件中,我希望所有50个按钮都有我使用设计器属性窗口设置的默认图像。这是可能的还是我应该再次声明图像???

    我尝试过但没有成功的:

    Properties.Settings.Default.Reset();
    Properties.Settings.Default.Reload();
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Adam Houldsworth    14 年前

    您需要再次在图像中手动设置:

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

    不过,您可以创建一个小方法来执行此操作,并传入一个按钮数组。我会制作一个所有按钮的本地窗体数组,以便访问。

        2
  •  2
  •   Adam Houldsworth    14 年前

    如果不缓存原始属性,则需要从资源中重新加载它们:

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

    或者,如果要重新加载所有零部件特性:

    var resources =
        new System.ComponentModel.ComponentResourceManager(typeof(Form1));
    resources.ApplyResources(button1, "button1");
    resources.ApplyResources(button2, button2.Name);
    ...