繁体   English   中英

PictureBox没有出现

[英]PictureBox doesn't appear

我有一个if语句,在该if语句内是一个foreach ,用于从string[]访问每个string

字符串是从文件中读取的NPC的一些参数。 第一个代表NPC类型,有两个:“战斗”和“示教”,“示教” NPC的最后一个string[]是“ end”,其余参数代表照片名称,我想在其中加载一个“对话框” PictureBox。

我的测试文件如下所示:

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语句。

解决这个错误似乎很容易,但是我不知道如何解决。

您可能需要为图片框发出PictureBox.Refresh和/或DoEvents命令,才能真正获得加载和显示图片的机会。

MessageBox自动执行DoEvents ...,这就是为什么它在调试期间可以正常工作的原因。

您现在正在执行的操作只是冻结UI。 请改用System.Windows.Forms.Timer 将计时器从工具箱拖放到窗体上。

然后创建一些计时器可以访问的字段,以存储您的图片和当前图片位置:

private List<string> pics = new List<string>();
private int currentPic = 0;

最后,用您要显示的图片加载它,然后启动计时器进行浏览:

pics.Clear();
pics.AddRange(date.Where(x => x != "teach" && x != "end"));
timer1.Interval = 5000;
timer1.Start();

然后,您必须告诉计时器显示下一张图片。 增加计数器,并在必要时将其重置。 这样的事情应该起作用。 根据需要进行修改。

private void timer1_Tick(object sender, EventArgs e)
{
    dialog.ImageLocation = string.Format("{0}/npc/{1}/{2}.png", folder, score_npc, pics[currentPic]);

    currentPic++;

    if (currentPic >= pics.Count)
        currentPic = 0;

    // Alternatively, stop the Timer when you get to the end, if you want
    // if (currentPic >= pics.Count)
    //     timer1.Stop();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM