等等然后在C#中执行代码?

时间:2012-05-18 16:21:15

标签: c# visual-c#-express-2010

好的,我的表格上有一个结束活动。其中的代码将表单淡出,然后打开另一个表单。有没有办法可以等到淡入淡出完成?这是我的代码:

    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(); 
        }
    }

基本上,form2提前打开,我希望它在淡入淡出效果完成后等待。 :)

2 个答案:

答案 0 :(得分: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.
    }

答案 1 :(得分:0)

代码:

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

这是一个Wait功能,你可以使用它来计算衰落期间的总时间,如下所示:

Wait(Time)