2012-05-18 29 views
1

好的,我在我的表单上有一个关闭事件。它内部的代码淡出表单,然后打开另一个表单。有什么办法可以等到淡出完成?这里是我的代码:等待然后在C#中执行代码?

private void form1_closing(object sender, FormClosingEventArgs e) 
    { 
     if (this.Opacity > 0.01f) 
     { 
      e.Cancel = true; 
      timer1.Interval = 47; 
      timer1.Enabled = true; 
      timer1.Start(); 

     } 
     else 
     { 
      timer1.Enabled = false; 
     } 
    } 

    private void timer2_Tick(object sender, EventArgs e) 
    { 
     progressBar1.Increment(+1); 
     label4.Text = progressBar1.Value.ToString() + "%"; 
     if (progressBar1.Value == 100) 
     { 
      progressBar1.Value = 100; 
      timer2.Stop(); 
      this.Close(); 
      Form2 frm2 = new Form2(); 
      frm2.Show(); 
     } 
    } 

基本上,窗口2时开通初期,我希望它等待的淡入淡出效果完成后。 :)

+2

你说,表格过早关闭? –

+0

你可以简单地设置一个像'form1_faded = true'这样的全局变量,并在第二次调用中检查它。 –

+0

好吧,是的,没有,表单在进度条实际显示为100之前关闭。另外,在淡出完成之前,form2弹出。 –

回答

2

这个代码应该做的,我猜。

private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     for (double i = 1.0; i > 0.0; i-=0.01) 
     { 
      this.Opacity = i; 
      Thread.Sleep(10); 
     } 

    //Handle whatever you want to do here. The control comes here only when the form is faded out. 
    } 
0

代码:

void Wait(int Milliseconds) 
{ 
    Stopwatch sw = new Stopwatch(); 
    sw.Start(); 
    while(sw.ElapsedMillisecods < Millisecods) 
    { 
     Application.DoEvents(); 
    } 
    sw.Stop(); 
} 

这里是一个Wait功能,你可以使用它,如果你可以计算出衰落这样的过程中所花费的总时间:

Wait(Time) 
相关问题