【问题标题】:Change BackgroundImage of form from Custom Control running in Panel从面板中运行的自定义控件更改表单的 BackgroundImage
【发布时间】:2016-01-01 23:45:18
【问题描述】:

我的项目由一个带有一个面板的表单组成,该面板包含一个自定义控件。 在这个自定义控件中,我有更改背景图像的按钮。

我的问题是;这些按钮仅更改它们所在的自定义控件的背景图像,我需要它们更改包含带有自定义控件的面板的主窗体的背景图像。

我当前的代码:

this.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));

我需要一些能够有效完成此任务的东西:

MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));

即:将MainForm.cs的背景图片从CustomControl.cs更改

【问题讨论】:

标签: c# image winforms background controls


【解决方案1】:

你可以使用Control.FindForm方法,像这样

this.FindForm().BackgroundImage = ...

【讨论】:

    【解决方案2】:

    我最终使用了不同的东西:

    Form MainForm = Application.OpenForms["(The name of the form in which I wanted to change the background)"];
    
    //...
    
    MainForm.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
    

    结果比我想象的要简单得多。

    【讨论】:

      【解决方案3】:

      找到控件的父级并更改BackgroundImage

      if(this.Parent!=null && this.Parent.Parent!=null)
         this.Parent.parent.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
      

      【讨论】:

        猜你喜欢
        • 2014-12-06
        • 2013-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多