簡體   English   中英

如何在循環中僅顯示一次消息框

[英]How to display messagebox only one time in a loop

我是C語言的新手

我正在使用消息框顯示該行。 但它處於循環狀態,因此消息框顯示的次數更多,我只想顯示一次,而消息框不應再次顯示。

try{
    using (var sr = File.OpenText("test.txt")){
        string newone = 20110501;//date
        string line;
        while ((line = sr.ReadLine()) != null){
            if (three => one){
                MessageBox.Show("Buy : " + line);
                //Its Displaying more than 50 times i want to displayit only
                //onetime and should dispose for the next Time
            }
        }
    }
} catch { }

提前致謝。

用這個:

try {
    using (var sr = File.OpenText("test.txt")) {
        bool flag = true;
        string newone = 20110501;//date
        string line;
        while ((line = sr.ReadLine()) != null) {
            if (flag) {
                MessageBox.Show("Buy : " + line);
                //Its Displaying more than 50 times i want to displayit only
                //onetime and should dispose for the next Time
            }
            flag = false;
        }
    }
} catch { }

很簡單,有一個布爾標志說明消息框是否已經顯示,並用它來保護該動作。

更新:

bool shown = false;
...
if( !shown )
{
     MessageBox.Show("Buy : " + line);
     shown = true;
}

您正試圖break; 從循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM