Hi all,

I am developing a windows application.

I have 3 forms:

I want to change the backcolor of all the 3 forms to the color selected by the user.

I have written the following code to do this. But the backcolor is not changed.

In Form1

 ColorDialog c1 = new ColorDialog();
        public System.Drawing.Color bkc;
        private void button1_Click(object sender, EventArgs e)
        {
            c1.ShowDialog();
            bkc = c1.Color;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 obj1 = new Form2();
            obj1.Show();
        }

In Form 2

private void button1_Click(object sender, EventArgs e)
        {
            Form3 obj1 = new Form3();
            obj1.Show();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Form1 obj2 = new Form1 ();
            this.BackColor = obj2.bkc;
        }

In Form3

private void button1_Click(object sender, EventArgs e)
        {
            Form1 obj1 = new Form1();
            obj1.Show();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            Form1 obj2 = new Form1();
            this.BackColor = obj2.bkc;
        }

In the color dialog box I am selecting a color but the color and pressing Ok button but the color is not changed.
The above code does not generate any error.

can anybody help me out in performing this task?

Thanks in advance!

Recommended Answers

All 2 Replies

If you want to change colour of all the forms onload of the form, then write assign the colour of the form in the onload function of the form.

In form1

        public static System.Drawing.Color x1;
        public Form1()
        {
            InitializeComponent();
            ColorDialog cd = new ColorDialog();
            cd.ShowDialog();
            x1 = cd.Color;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 oj = new Form2();
            oj.Show();
        }

In form2
               public Form2()
		{
			InitializeComponent();
                        this.BackColor = Form1.x1;
		}

You can do the same for all the forms and i didn't understand why u have to create a new object of form1 in form3.

Hope this solves your problem.

Try this code out. You must build it before you can view the forms in the designer -- so when you first open it just close all the files, do a build, then use the designer. "FormEx.cs" is a descendant of "Form" and is just a class file -- not a form. You need to right click on it and "View Code".

This uses static skinning for form colors. Here is the guts of the logic:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace daniweb.frmcolor
{
  public class FormEx : Form
  {
    /// <summary>
    /// Set the default color for the designer
    /// </summary>
    static FormEx()
    {
      _globalBackgroundColor = default(Color?);
    }

    private static void InvalidateForms()
    {
      try
      {
        for (int i1 = 0; i1 < Application.OpenForms.Count; i1++)
        {
          try
          {
            FormEx frm = (Application.OpenForms[i1] as FormEx);
            if (frm != null)
            {
              frm.Invalidate(true);
              frm.Refresh();
            }
          }
          catch 
          { 
            //Should never happen
          }
        }
      }
      catch
      {
        //this will catch if the form count changes
      }
    }

    private static Color? _globalBackgroundColor;
    /// <summary>
    /// Sets the background color for all forms
    /// </summary>
    public static Color? GlobalBackgroundColor
    {
      get { return FormEx._globalBackgroundColor; }
      set 
      {
        if (FormEx._globalBackgroundColor != value)
        {
          FormEx._globalBackgroundColor = value;
          InvalidateForms();
        }
      }
    }

    public override Color BackColor
    {
      get
      {
        return (_globalBackgroundColor == null ? base.BackColor : (Color)_globalBackgroundColor);
      }
      set
      {
        base.BackColor = value;
      }
    }
 
    /// <summary>
    /// Create a new colored form
    /// </summary>
    public FormEx()
      : base()
    {
    }


  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.