从面板中的自定义控件 运行 更改表单的背景图像

Change BackgroundImage of form from Custom Control running in Panel

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

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

我当前的代码:

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

我需要一些可以有效实现此目的的东西:

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

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

更改为

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

this.FindForm().BackgroundImage = ...

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

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

结果比我想象的简单多了。

找到控件的父控件并更改 BackgroundImage:

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