开发者

How to display messagebox only one time in a loop

开发者 https://www.devze.com 2023-04-08 00:19 出处:网络
I am New In C sharp, 开发者_JS百科 I am using messagebox to display the line. but it is in a loop so the messagebox display more times i want to display it only one time and message box should not be

I am New In C sharp,

开发者_JS百科

I am using messagebox to display the line. but it is in a loop so the messagebox display more times i want to display it only one time and message box should not be display for the next time.

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 { }

Thanks In Advance.


use this:

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 { }


Simple have a boolean flag saying whether the message box has already been shown or not, and use it to guard that action.

Update:

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


You're trying to break; from the loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号