0

My project consists of a form with a panel that contains a custom control. In this custom control I have buttons that change the background image.

My issue is; these buttons only change the background image of the custom control that they are placed in, and I need them to change the background image of the main form containing the panel with the custom control.

My current code:

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

I need something that will in effect accomplish this:

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

ie: Change background image of MainForm.cs from CustomControl.cs

Steve
  • 213,761
  • 22
  • 232
  • 286
Patrick
  • 430
  • 6
  • 21

3 Answers3

2

You can use Control.FindForm method for that, like this

this.FindForm().BackgroundImage = ...
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
0

I ended up using something different:

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

It ended up being a lot simpler than I thought it was.

Patrick
  • 430
  • 6
  • 21
0

Find the control's parents and change the BackgroundImage:

if(this.Parent!=null && this.Parent.Parent!=null)
   this.Parent.parent.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
Ghasem
  • 14,455
  • 21
  • 138
  • 171