2017-04-23 77 views
0

當我在文本框上寫錯了某些內容並單擊某個按鈕時,彈出一個消息框,並且由於有定時器而不斷彈出。C#如果顯示消息框,然後停止計時器

所以我想做一個if語句,如果顯示messagebox,然後停止計時器,直到再次單擊該按鈕。

我嘗試使用這樣的:

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

我還會注意到我使用errormsg在我的//代碼的其他語句中使用

//code 
    else 
      { 
       errormsg(); 
      } 

所以我的問題是:

我怎樣才能讓計時器停止,如果顯示在我的文本框(//碼)造成一個消息框出現一個錯誤的值。然後,當一個正確的值顯示在一個文本框上,然後點擊該按鈕,定時器會再次啓動?

回答

1

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