【发布时间】:2015-05-14 04:35:05
【问题描述】:
我有一个if 语句,里面的if 语句是一个foreach,用于从string[] 访问每个string。
字符串是从文件中读取的 NPC 的一些参数。第一个代表NPC类型,有两个:“battle”和“teach”,“teach”NPC的最后一个string[]是“end”,其余参数代表我要加载的照片名称一个“对话框”图片框。
我的测试文件如下所示:
teach
poza1
poza2
end
所以我有 2 张照片要加载到对话框 PictureBox 中。想法是我必须暂停那条foreach 语句5 秒,否则对话框PictureBox 图片会加载得太快,我看不到它们。
所以我尝试这样做,代码如下所示:
if (date[0].Equals("teach")) //the first line of the date[] string, date represent the text from the file
{
foreach (string parametru in date) // i think that you know what this does
{
if (parametru != "teach" && parametru != "end") // checking if the parameter isn't the first or the last line of the file
{
dialog.ImageLocation = folder + "/npc/" + score_npc + "/" + parametru + ".png"; //loading the photo
System.Threading.Thread.Sleep(5000);
}
}
//other instructions , irelevants in my opinion
}
在尝试调试时,我意识到如果我使用MessageBox,该函数将加载两张照片。此外,我确信参数将通过 if 语句。
修复这个错误似乎很容易,但我不知道该怎么做。
【问题讨论】:
-
在致电 Sleep 之前,请致电
Application.DoEvents(),尽管这不受欢迎并且通常不是正确的做法。从技术上讲,您想做一个dialog.Invoke并在委托过程中设置图像。 -
@DanielA.White 是的,这是在 winforms 中
-
您在 pb 有时间显示它们之前加载图像。如果你想使用某种动画 Timer 会更好,但如果你在 thread.Sleep 之前调用 PB.Refresh 它应该也可以工作..
-
@RonBeyer 如何在委托过程中设置图像
-
类似
dialog.Invoke(delegate { dialog.ImageLocation = ...; });
标签: c# winforms picturebox