简体   繁体   English

C#如果显示消息框,则停止计时器

[英]C# If messagebox is displayed, then stop timer

When I write something wrong on a textbox and click a button, a messagebox pops up, and keeps popping up since I have a timer. 当我在文本框中输入错误内容并单击按钮时,会弹出一个消息框,并且由于我有计时器,所以该消息框会不断弹出。

So I want to make an if statement that if the messagebox is displayed, then stop the timer, until the button is clicked once again. 因此,我想做一个if语句,如果显示消息框,则停止计时器,直到再次单击该按钮。

I tried using this: 我尝试使用此:

private void button1_Click(object sender, EventArgs e)
    {

        timer1.Start();
        if (errormsg)
        {
            timer1.Stop();
        }
        data();

    }
    private void data()
    {
     //code

Now here's what's in my timer1 code: 现在这是我的timer1代码中的内容:

 private void timer1_Tick(object sender, EventArgs e)
    {
        int value;

        if (int.TryParse(textBox1.Text, out value))
        {
            if (value > 0)
            {
                timer1.Interval = value;
            }
        }
        button1.PerformClick();
    }

here's the error message: 这是错误消息:

private void errormsg()
    {
        MessageBox.Show("Sorry, there was an error. Please, try again.");
    }

I will also note that I'm using errormsg in an else statement on my //code 我还会注意到,我在// code上的else语句中使用errormsg

//code
    else
            {
                errormsg();
            }

So my question is: 所以我的问题是:

How can I make the timer stop, if a wrong value is displayed on my textbox (//code) causing a messagebox to appear. 如果我的文本框(//代码)上显示的值错误导致出现消息框,如何使计时器停止计时。 Then, when a correct value is displayed on a textbox, and I click the button, the timer would start again? 然后,当在文本框中显示正确的值并单击按钮时,计时器会重新启动吗?

Stop the timer in your errormsg() function. 在您的errormsg()函数中停止计时器。 When you clicked the button1 it starts again. 当您单击button1时,它将再次启动。

private void button1_Click(object sender, EventArgs e)
    {
        timer1.Start();
        data();
    }

private void errormsg()
    {
        timer1.stop();
        MessageBox.Show("Sorry, there was an error. Please, try again.");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM