我是C夏普的新人
我正在使用messagebox来显示这行。但是它在一个循环中,所以消息框显示了更多的次数,我想只显示一次,并且消息框不应该在下一次显示。
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 { }提前谢谢。
发布于 2011-09-25 12:25:08
使用以下命令:
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 { }发布于 2011-09-25 12:22:18
简单地说,有一个布尔标志,表明消息框是否已经显示,并使用它来保护该操作。
更新:
bool shown = false;
...
if( !shown )
{
MessageBox.Show("Buy : " + line);
shown = true;
}发布于 2011-09-25 12:23:54
您正在尝试从循环中执行break;。
https://stackoverflow.com/questions/7543633
复制相似问题