如果在指定时间内未单击按钮,则关闭表单并打开第二个表单

时间:2018-10-02 03:48:20

标签: c# winforms timer

我正在使用两个Windows窗体应用程序:窗体1和窗体2。窗体2是运行时出现的第一个窗体,它提示用户单击一个按钮来启动窗体1。目的是,如果几秒钟内未单击该按钮,则此表单将关闭,并且表单1将打开。

我以为我的代码可以达到最佳工作状态,但是当我以多种方式对其进行测试时却不是。第一次测试,等待5秒而不按开始按钮,将打开Form 1。但是,当我单击开始按钮时,将打开表格1,并在5秒钟内打开另一个表格1。这不是我所期望的。我只想打开一个Form 1。

我的代码如下所示:

计时器t2 =新的Timer();  私人布尔startButtonWasClicked = false;

    public Form2()
    {
        InitializeComponent();
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        startButtonWasClicked = true;

        bool IsOpen = false;
        foreach(Form f in Application.OpenForms)
        {
            if(f.Text == "Form1")
            {
                IsOpen = true;
                f.Focus();
                break;
                //if the form is already open, it will focus on that form
            }
        }

        if (IsOpen == false)
        {
            Form1 f1 = new Form1();
            f1.Show();
        }
        //only one Form1 will be allowed when Start button is clicked.
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        //if the start button isn't pressed, in 5 seconds, Form2 closes and Form1 opens
        if (startButtonWasClicked)
        {
            t2.Interval = 5000;
            t2.Tick += new EventHandler(OnTimerTicker);
            t2.Start();
        }
        //else start button is clicked

    }

    private void OnTimerTicker(object sender, EventArgs e)
    {

            t2.Stop();
            Form1 f1 = new Form1();
            this.Hide();//"closes form 2 after 5 seconds and opens form 1
            f1.Show();

    }

我不确定是否遗漏了重要部分,但是从测试结果的外观来看,肯定有一些遗漏。

2 个答案:

答案 0 :(得分:1)

我认为您说的与您的代码有所不同。假设您的意思是您在说的话,那么您在此处的if语句应该相反:

private void Form2_Load(object sender, EventArgs e)
    {
        //if the start button isn't pressed, in 5 seconds, Form2 closes and Form1 opens
        // Don't really need this because a user won't be able to click the button before the form loads
        if (!startButtonWasClicked)
        {
            t2.Interval = 5000;
            t2.Tick += new EventHandler(OnTimerTicker);
            t2.Start();
        }
        //else start button is clicked

    }

可以看到here,这是WinForms的顺序,如果您感兴趣的话,可以查看一下,但是重要的一点是,当第一个窗体Form 2被调用时,将调用Load()方法。开了从处理程序开始计时。

即使用户随后单击,一旦他们到达处理程序('OnTimerTicker'),您都将需要一个条件来检查是否应打开另一个Form1。像这样:

private Timer t2 = new Timer();
        private bool startButtonWasClicked = false;

        private void Form2_Load(object sender, EventArgs e)
        {
            //if the start button isn't pressed, in 5 seconds, Form2 closes and Form1 opens
            if (!startButtonWasClicked)
            {
                t2.Interval = 5000;
                t2.Tick += new EventHandler(OnTimerTicker);
                t2.Start();
            }
            //else start button is clicked
        }



        private void button1_Click(object sender, EventArgs e)
        {
            startButtonWasClicked = true;
            // Stop the timer so it doesn't still run
            t2.Stop();

            bool IsOpen = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Text == "Form1")
                {
                    IsOpen = true;
                    f.Focus();
                    break;
                    //if the form is already open, it will focus on that form
                }
            }

            if (IsOpen == false)
            {
                Form1 f1 = new Form1();
                f1.Show();
            }

            // Hide this window to be consistent
            this.Hide();
        }

        private void OnTimerTicker(object sender, EventArgs e)
        {
            if (startButtonWasClicked) { return; }
            t2.Stop();
            Form1 f1 = new Form1();
            this.Hide();//"closes form 2 after 5 seconds and opens form 1
            f1.Show();

        }

您还可以改善其他方面,以重构并始终隐藏/不隐藏,但是我认为这可以解决您的问题。

答案 1 :(得分:0)

btnStart_Click中单击按钮时,您可能需要停止计时器。

我看到的是计时器,它是在Form2_Load中加载了 Form2 时启动的。因此,如果单击了按钮,则需要停止计时器,否则它将按设置的间隔触发。