簡體   English   中英

C#如果顯示消息框,則停止計時器

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

當我在文本框中輸入錯誤內容並單擊按鈕時,會彈出一個消息框,並且由於我有計時器,所以該消息框會不斷彈出。

因此,我想做一個if語句,如果顯示消息框,則停止計時器,直到再次單擊該按鈕。

我嘗試使用此:

private void button1_Click(object sender, EventArgs e)
    {

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

    }
    private void data()
    {
     //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();
    }

這是錯誤消息:

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

我還會注意到,我在// code上的else語句中使用errormsg

//code
    else
            {
                errormsg();
            }

所以我的問題是:

如果我的文本框(//代碼)上顯示的值錯誤導致出現消息框,如何使計時器停止計時。 然后,當在文本框中顯示正確的值並單擊按鈕時,計時器會重新啟動嗎?

在您的errormsg()函數中停止計時器。 當您單擊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