C# 我需要在10次循环后将背景色从白色/绿色闪烁更改为白色/红色闪烁
我有一个VisualStudio表单,我需要在10个循环后将背景颜色从白色/绿色闪烁更改为白色/红色闪烁C# 我需要在10次循环后将背景色从白色/绿色闪烁更改为白色/红色闪烁,c#,C#,我有一个VisualStudio表单,我需要在10个循环后将背景颜色从白色/绿色闪烁更改为白色/红色闪烁 private void Timer1_Tick(object sender, EventArgs e) { if (this.BackColor == Color.LightGreen) { this.BackColor = Color.White; } else {
private void Timer1_Tick(object sender, EventArgs e)
{
if (this.BackColor == Color.LightGreen)
{
this.BackColor = Color.White;
}
else
{
this.BackColor = Color.LightGreen;
}
}
这段代码可以工作,但它只执行前半部分,我还没有找到其他方法来执行。使用会话变量(代码未测试…但类似于)
我猜你的意思是在计时器响10下之后?你如何计算用户点击按钮的次数?没错!我的第一个想法是使用一个整数变量,并在每个刻度后加上+1,尽管我没有让它工作。
我没有让它工作。
给我们看看这个代码。你的思路是正确的。创建一个类字段,我们称之为“tickcount”,并在每个tick中递增它。然后你可以检查它的值来决定你想要红色还是绿色。这基本上是抄袭@jamezfatut的答案,它不能按照问题中的要求工作。这怎么能被接受呢?
...
if (this.BackColor == Color.LightGreen && Session[Click] == 10)
{
int count = Session.[Click];
this.BackColor = Color.White;
Session[Click] = count +1;
}
...
public int count = 0;
private void Timer1_Tick(object sender, EventArgs e)
{
this.count++;
if(this.count == 10)
{
if (this.BackColor == Color.Red)
{
this.BackColor = Color.White;
}
else
{
this.BackColor = Color.Red;
}
}
else {
if (this.BackColor == Color.LightGreen)
{
this.BackColor = Color.White;
}
else
{
this.BackColor = Color.LightGreen;
}
this.count = 0;
}
}