2012-05-18 67 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) 
相關問題