当计时器完成增量时间后,我想关闭该表格(计时器表格) [英] When timer finish the time of increment i want to close that Form(timer Form)

查看:60
本文介绍了当计时器完成增量时间后,我想关闭该表格(计时器表格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当计时器完成增量时,我想关闭Form2的那个窗体



when timer finished increment i want to close that form that is Form2



namespace WindowsFormsAppDAL
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        Form3 f3 = new Form3(); 

        private void timer1_Tick(object sender, EventArgs e)
        {

            if (progressBar2.Value < 100)
            {
                progressBar2.Value += 2;
            }
            else
            {
                timer1.Stop(); 
                f3.ShowDialog();
            }
            
           
        }
    }
}

推荐答案

您不能,不能使用该代码. ShowDialog是一个模式调用,这意味着它要等到显示的窗体关闭后才能继续执行-这意味着您不能在form3完成之前关闭form2.如果使用Show代替,它将立即返回,并且可以在窗体上使用Close,但这将产生不可预测的效果,因为form3的实例是form2的一部分,并且如果其中一个被销毁,那么另一个也可以.
有两种方法可以满足您的需求:
1)隐藏表格2,并在以下步骤后将其关闭:
You can''t, not with that code. ShowDialog is a modal call, which means that it waits until the form being shown has closed before it continues executing - which means you can''t close the form2 until form3 is finished. If you used Show instead, it would return immediately and you could use Close on the form, but that would have unpredictable effects since the instance of form3 is a part of form2, and if one is destroyed, then the other could go too.
There are two approaches which could do what you want:
1) Hide your form2, and close it after:
private void timer1_Tick(object sender, EventArgs e)
{

    if (progressBar2.Value < 100)
    {
        progressBar2.Value += 2;
    }
    else
    {
        timer1.Stop();
        Hide();
        f3.ShowDialog();
        Close();
    }
}


2)通过创建父级处理的事件,并在关闭自己之前发出信号,向父级发送信号,告知您已完成,并希望显示form3.然后,父母决定显示该表格,一切都会愉快地进行.

我会选择第二个选项,这样可以更好地隔离事物.


2) Signal back to the parent form that you are finished, and would like form3 to be shown, by creating an event which the parent handles, and signalling it before closing yourself. The parent then decides to show the form and everything works happily.

I would go with the second option, and it isolates things better.


private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Value += 1;

            if (progressBar1.Value == progressBar1.Maximum)
            {
                timer1.Enabled = false;
                this.Close();            
            }

        }


这篇关于当计时器完成增量时间后,我想关闭该表格(计时器表格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆