简体   繁体   English

C#使用按钮更改表单的背面颜色

[英]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? 在Visual Studio 2017社区中,如何在按下按钮后更改表单的背景色? 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 我的错误

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM