【问题标题】:How can I make a button which swaps between 3 background colors?如何制作一个在 3 种背景颜色之间切换的按钮?
【发布时间】:2021-09-08 09:54:55
【问题描述】:

所以我试图弄清楚如何在 Windows 应用程序表单中单击按钮在 3 种背景颜色之间切换。设法让它在 2 个之间交换,但很难移动到 3 个。

尝试了大约 30 分钟后想到的所有结果:https://prnt.sc/1rka67i

2 种颜色变化:https://prnt.sc/1rkakrm

【问题讨论】:

  • 到目前为止你尝试过什么?贴出代码
  • 编辑:添加图片
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: c# windows if-statement nested-if


【解决方案1】:

在这种情况下,您可以在按钮单击事件上修改表单背景的属性,例如:

private void button1_Click(object sender, EventArgs e)
{
    this.BackColor = Color.White; //for example 
}

如果您想在 3 种颜色之间进行交换,您可以尝试:

int counter = 0;
private void button1_Click(object sender, EventArgs e)
{
    counter++;
    if(counter == 1)
        this.BackColor = Color.White; //for example 
    if(counter == 2)
        this.BackColor = Color.Black; //for example 
    if(counter == 3){
        this.BackColor = Color.Gray; //for example 
        counter = 0;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-07
    • 2017-09-22
    • 2020-09-14
    • 1970-01-01
    • 2017-08-19
    • 2021-03-28
    • 2017-12-28
    • 1970-01-01
    相关资源
    最近更新 更多