C# Timer控件 事件不起效果

来源:百度知道 编辑:UC知道 时间:2024/06/08 18:18:33
private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Text = this.Opacity.ToString();
this.Opacity -= 0.01;

if (this.Opacity<=0)
{
this.Opacity += 0.01;

}
我想让窗体从不透明 到透明,再到不透明 这样循环 但是去只能到0完全透明就没继续了...哪里出问题了

看代码可知
当Opacity==0 的时候,Opacity 被赋值为0.01,紧接着在下一次程序执行中,Opacity 又变成0.所以看到完全透明。
这样修改
private void timer1_Tick(object sender, EventArgs e)
{
this.label1.Text = this.Opacity.ToString();
this.Opacity -= 0.01;

if (this.Opacity<=0)
{
Opacity=1;
}
}

你每次都先减掉0.01,再加上0.01
透明度总是保持在0.01,你当然看不见form了
如果要渐进的从不透明 到透明,再到不透明
那么应该设置标志位,到0的时候开始加0.01
到1的时候再开始减0.01
private bool increase;
private bool decrease;
private void timer1_Tick(object sender, EventArgs e)
{
this.textBox1.Text = this.Opacity.ToString();

if (this.Opacity <= 0)
{
increase = true;
decrease = false;
}

if(this.Opacity >= 1)
{