首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在循环中只显示一次messagebox

如何在循环中只显示一次messagebox
EN

Stack Overflow用户
提问于 2011-09-25 12:20:10
回答 3查看 10.2K关注 0票数 0

我是C夏普的新人

我正在使用messagebox来显示这行。但是它在一个循环中,所以消息框显示了更多的次数,我想只显示一次,并且消息框不应该在下一次显示。

代码语言:javascript
复制
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 { }

提前谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-25 12:25:08

使用以下命令:

代码语言:javascript
复制
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 { }
票数 5
EN

Stack Overflow用户

发布于 2011-09-25 12:22:18

简单地说,有一个布尔标志,表明消息框是否已经显示,并使用它来保护该操作。

更新:

代码语言:javascript
复制
bool shown = false;
...
if( !shown )
{
     MessageBox.Show("Buy : " + line);
     shown = true;
}
票数 3
EN

Stack Overflow用户

发布于 2011-09-25 12:23:54

您正在尝试从循环中执行break;

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7543633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档