[英]How to display messagebox only one time in a loop
I am New In C sharp, 我是C语言的新手
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;
您正试图break;
from the loop. 从循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.