Search code examples
c#formsbuttontimerbackcolor

C# Change The Form's Back Color With A Button


In Visual Studio 2017 Community, how can I change the back colour of the form once a button is pressed? I am trying to make a rainbow form so in that instance I am using a timer.

My code is:

        private void timer1_Tick(object sender, EventArgs e)
    {
        Random rand = new Random();
        int A = rand.Next(0, 255);
        int R = rand.Next(0, 255);
        int G = rand.Next(0, 255);
        int B = rand.Next(0, 255);
        Form1.BackColor = Color.FromArgb(A, R, G, B);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Start();
    }
}

But that doesn't work.

My Error


Solution

  • This works!

    private void timer1_Tick_1(object sender, EventArgs e)
        {
            Random rand = new Random();
            int A = rand.Next(0, 255);
            int R = rand.Next(0, 255);
            int G = rand.Next(0, 255);
            int B = rand.Next(0, 255);
            this.BackColor = Color.FromArgb(255, R, G, B);
        }