0

Hello:)

I can change background colour on my existing form like this:

this.BackColor = System.Drawing.Color.Green;

or like this

ActiveForm.BackColor = System.Drawing.Color.Green;

But I have another form called CustomerData
What I want to know is how I go about using a button click event on the first form to change the background colour on the CustomerData form?

Thank you ... John.

2
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by johnt68
Featured Replies
  • If your form is already opened: [CODE] //form1: public partial class Form1 : Form { Form2 f2; public Form1() { InitializeComponent(); } //open form2 someplace else!! //before calling event bellow! private void button2_Click(object sender, EventArgs e) { f2.UpdateFormColor(); } } //form2: public partial class Form2 : Form { public Form2() … Read More

  • Did you check the whole code? The code works, I guess you didnt see the "Form2 f2;" variables on top of the constructor of form1. [CODE] public partial class Form1 : Form { [B]Form2 f2;[/B] public Form1() { InitializeComponent(); } //open form2 someplace else!! //before calling event bellow! private void … Read More

1

If your form is already opened:

//form1:
    public partial class Form1 : Form
    {
        Form2 f2;
        public Form1()
        {
            InitializeComponent();
        }

        //open form2 someplace else!!
        //before calling event bellow!
        private void button2_Click(object sender, EventArgs e)
        {
            f2.UpdateFormColor();
        }
    }

//form2:
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public void UpdateFormColor()
        {
            this.BackColor = Color.YellowGreen;
        }
    }

Hope it helps,
Mitja

Comments
Constant effort.
0

Hi Mitja,

Thanks for the help. Sorry I should have been clearer from the start. I said one form because I thought that it would make things easier - but I have about ten forms. Some may be open and some may be closed. The idea is that I can have a 'change theme form' holding three buttons that changes all the form backgrounds to any one of those three colour themes when clicked. I don't mind if the background colour doesn't change until the form is closed and reopened again.

I have tried to imitate the above but am getting errors - where it says Form2 I get the error: A namespace cannot directly contain members such as fields or methods. And where it says f2.UpdateFormColor(); I get an error on the f2 that says Error: The name 'f2' does not exist in the current context

But I know that I'm probably missing something - so thanks again ... John - I'll keep trying :0)

Edited by johnt68: n/a

1

Did you check the whole code? The code works, I guess you didnt see the "Form2 f2;" variables on top of the constructor of form1.

public partial class Form1 : Form
    {
        [B]Form2 f2;[/B]
        public Form1()
        {
            InitializeComponent();
        }
 
        //open form2 someplace else!!
        //before calling event bellow!
        private void button2_Click(object sender, EventArgs e)
        {
            f2.UpdateFormColor();
        }
    }

The same goes if you have more forms open. Get all opened forms and create that same method on other forms, and call them:

public partial class Form1 : Form
    {
        [B]Form2 f2;
        Form3 f3;[/B]//you need to have this instances of the forms visible to all class so they can be used!
        public Form1()
        {
            InitializeComponent();           
        }

        private void SetColorToForms()
        {
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name = "Form2")
                    f2.UpdateFormColor();
                else if(form.Name =="Form3")
                    f3.UpdateFormColor();
            }
        }
    }
Comments
Extremely helpful ... and patient - thank you.
0

Hi Mitja,

Thanks again for the help. My mistake was more basic I am afraid - I had put

Form2 f2;

in the wrong place ... like this

namespace NWTG_TicketBookingSystem
{   
    Backstage backstage;
         public partial class ChangeTheme : Form
    {

It was obvious when I looked again with fresh eyes. I have now put the forms in the correct place and you are quite right - the code does work - so thank you for that. But, this involves clicking a button on the second form. What I really want to do is a give the choice of three colours on form one, then, for example, if the red button is clicked, then it changes the colour of form 2 to a red background (and all other forms) without any more button clicking on the second form. Is that possible ?

John.

Edited by johnt68: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.