Madhavan
Madhavan

Reputation: 230

Set the same background in all windows forms with a single setting

i have 40 windows forms in a C# Project. i have to set the same background in all forms. is any option or settings to do that in a single settings?

Upvotes: 3

Views: 2376

Answers (5)

Ajeeli
Ajeeli

Reputation: 373

We solved this issue by adding the color code in your base form load function, which is inherited to child forms without issues.

        private void BaseForm_Load(object sender, EventArgs e)
    {
            // put your color code here
    }

Upvotes: 0

Nathan Baulch
Nathan Baulch

Reputation: 20683

Inheritance is the right way to go about this, but sometimes it's fun to do things the wrong way too (though not in production of course):

var type = typeof (Color).Assembly.GetType("System.Drawing.KnownColorTable");
var field = type.GetField("colorTable",
                          BindingFlags.NonPublic | BindingFlags.Static);
var colorTable = (int[]) field.GetValue(null);
colorTable[(int) KnownColor.Control] = Color.Blue.ToArgb();
colorTable[(int) KnownColor.ControlText] = Color.Red.ToArgb();
new Form {Controls = {new Button {Text = "Success!"}}}.ShowDialog();

Upvotes: 0

AlSki
AlSki

Reputation: 6961

Just as an alternative, if you cannot use inheritence then you can refactor the code that applies the styling into a single place and then you only need to apply one line to every form. In fact with a little syntactic sugar (aka an extension method) you can make it look it was always part of your class anyway

i.e.

//Example form with an inheritor that blocks us from using inheritence to apply style
public class MainForm : 3rdPartyLibrary.WizardForm 
{
  public MainForm()
  {
    ApplyStyle();
  }
}

//Normal form 
public class MyDialog : Form
{
  public MyDialog()
  {
    ApplyStyle();
  }
}

public static class WinFormExtension
{
  public static void ApplyStyle(this Form form)
  {
    form.BackColor = Colors.NavyBlue;
    //etc
  }

Upvotes: 1

user1968030
user1968030

Reputation:

You can create base form and set the background for it and other form inherit from the base form.

class baseForm: Form {
    void base() {
        this.BackColor = //set here
    }
}

class YourForm : baseForm {

}

Upvotes: 3

Piggeldy
Piggeldy

Reputation: 16

Work with Inheritance.

Create a Basic form, set the background property and inherit with all forms to the Basicform.

But im not sure, if the Visual Studio Gui-Desginer can handle Inheritance.

Upvotes: 0

Related Questions