Real2000
Real2000

Reputation: 63

Change the back color of any open forms runtime in C#

according to this topic

i have a menustrip in C# (win app) project , that for clicking each item of it, a form opens! as i said, i have 37 forms and i want to do this : whenever a form opens inside the MainForm (Form1) by clicking menustrip's items, i want to set form's back color, but i don't want to do this one by one(handy),is there a way ?! thanks .

Upvotes: 2

Views: 8666

Answers (5)

Junaid
Junaid

Reputation: 639

You must have BaseForm Class like this that is inheriting Form Class

 public partial class BaseForm : Form
 {
    protected override void OnLoad(EventArgs e)
    {
       Color colBackColor =Properties.Settings.Default.FormsBackgroundColor;
       BackColor = colBackColor;
    }
  }

and MainForm class like this which is inheriting BaseForm Class.

public partial class MainForm : BaseForm
{
    private void button1_Click_1(object sender, EventArgs e)
    {
            ColorDialog colorDlg = new ColorDialog();
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.FormsBackgroundColor= colorDlg.Color;
                Properties.Settings.Default.Save();
                this.BackColor = colorDlg.Color;
            }
        }    
 }

All classes should inherit BaseForm Class instead of Form Class like i did in MainForm class here "public partial class MainForm : BaseForm"

Upvotes: 1

Francesco Baruchelli
Francesco Baruchelli

Reputation: 7468

It's not completely clear what you mean with a form opens inside of MainForm(Form1). If you are adding your Form to Form1's Controls, you can use the ControlAdded event of Form1 with something like this:

this.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.Control_Added);

private void Control_Added(object sender, System.Windows.Forms.ControlEventArgs e)
{
    if (e.Control is Form)
        ((Form)e.Control.BackColor = Color.Blue;
}

If yours is an Mdi app, you could use the MdiChildActivate event, instead.

EDIT:

Of course this approach will work only IF you add the inner forms to the main form's controls.

Anyway, I don't think that the way an application is designed should be driven by "I don't want to change 37 methods" and looking for tricks and workarounds. To me the better and cleaner solution in this case would be to avoid subclassing, events and whatever and simply implement a new method like this one in your main form:

private void PrepareAndShow(Form aForm)
{
    aForm.BackColor = Color.Blue;
    // here you can add (even in the future) all the preprocessing you want
    aForm.Show();
}

Then you will of course have to modify all the methods for the menu items calling it:

Form aFrm = new Form2();
this.PrepareAndShow(aFrm); // instead of aFrm.Show();

In this way you have to modify 37 methods (less than 10 minutes work, I guess), but keep your application clean, open to future changes and extensions. Everything else can be fun, and interesting to experiment with, but I would not use it in production code.

Upvotes: 0

Salvatore Previti
Salvatore Previti

Reputation: 9050

A possibility is to create a base form class that you can use in all your forms, from now on.

If you don't want to change your background color for all forms, instead of making your forms inheriting from Form, make them inherits from MyFormBaseClass.

There is no clean way to understand from what form you are opening a form, except you specify the owber form when you do myform.Show(this);

In MyFormBaseClass, override OnLoad and put something like this:

protected override OnLoad(EventArgs e)
{
    if (this.Owner is MyOwnerFormClass) { this.BackColor = Color.Blue; }
    base.OnLoad(e);
}

You can use this to add more complex operations too, all common to all your forms.

You can replace all form base classes with a search and replace in all your project.

You must create MyFormBaseClass as a normal form to keep the designer working.

Press CTRL+H, select replace in all project, replace " : Form" with " : MyFormBaseClass", press Next until you are done, that is, 37 times.

You can replace also .Show() with .Show(this) if you need.

Upvotes: 1

Random Dev
Random Dev

Reputation: 52280

first you have to store all the open forms in some kind of collection. For example:

private List<Form> _childForms = new List<Form>();

every time you open a form you add it to this collection and add a closed handler to remove it when it is closed:

void AddForm(Form toAdd)
{
   _childForms.Add(toAdd);
   toAdd.Closed += (sender, e) => { _childForms.Remove(toAdd); }
}

Now you can use

void SetBackgroundColorToAllChidForms(Color color)
{
   foreach(var form in _childForms)
      form.BackColor = color;
}

to change the color of each of them.

PS: If you add them as MDI-Children you can do without the collection and your this.MdiChildren instead:

void SetBackgroundColorToAllMdiChildren(Color color)
{
   foreach(var form in this.MdiChildren)
      form.BackColor = color;
}

Upvotes: 0

Tigran
Tigran

Reputation: 62246

Do you mean this ? :
In menu strip click event handler, where you actually going to shot the form..

Form myNewForm = new Form(); 
myNewForm .BackColor = Color.X;

After show your form...

Upvotes: 0

Related Questions