简体   繁体   中英

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.

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:

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

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. When you clicked the button1 it starts again.

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.");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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