2014-02-12 35 views
0
private void timer1_Tick(object sender, EventArgs e) 
{ 
    AnimateCheckBoxes(); 
} 

private void AnimateCheckBoxes() 
{ 
    for (int i = 0; i < ScrollLabel._lines.Length; i++) 
    { 
     if (ScrollLabel._lines[i].Contains("Test")) 
     { 
      MessageBox.Show(ScrollLabel._lines[i]); 
     } 

    } 
} 

我希望MessageBox.Show只顯示一次而不停止計時器我該怎麼做?如何在計時器繼續工作時只顯示一行?

如果多於一行包含字符串測試如何顯示兩行?

回答

3

只需使用一個instance boolean變量

private void timer1_Tick(object sender, EventArgs e) 
      { 
       AnimateCheckBoxes(); 
      } 
    private static bool displayed ; 
      private void AnimateCheckBoxes() 
      { 
       for (int i = 0; i < ScrollLabel._lines.Length; i++) 
       { 
        if (ScrollLabel._lines[i].Contains("Test")&&!displayed) 
        { 
         displayed = true; 
         MessageBox.Show(ScrollLabel._lines[i]); 

        } 

       } 
      } 
+0

其kkeping告訴我messagebox.show多次所有的時間在MessageBox的一個新窗口。 – user3200169

+0

確定只是把變量作爲靜態我更新了答案 –

+0

啊,因爲它的messagebox.show我需要點擊OK,如果沒有,它永遠不會真正的。 – user3200169

相關問題