简体   繁体   English

PictureBox没有出现

[英]PictureBox doesn't appear

I have an if statement, inside that if statement is a foreach , for accessing each string from a string[] . 我有一个if语句,在该if语句内是一个foreach ,用于从string[]访问每个string

The strings are some parameters for an NPC which are read from a file. 字符串是从文件中读取的NPC的一些参数。 The first one represents the NPC type which are two : "battle" and "teach", the last string[] for a "teach" NPC is "end", and the rest of parameters represents photo names, which I want to load in a "dialog" PictureBox. 第一个代表NPC类型,有两个:“战斗”和“示教”,“示教” NPC的最后一个string[]是“ end”,其余参数代表照片名称,我想在其中加载一个“对话框” PictureBox。

My test file looks like this: 我的测试文件如下所示:

teach
poza1
poza2
end

So I have 2 photos to load in the dialog PictureBox. 所以我有2张照片要加载到对话框PictureBox中。 The idea is that I must pause for 5 seconds that foreach statement, otherwise the dialog PictureBox pictures will be loaded too fast, and I won't see them. 我的想法是我必须暂停foreach语句5秒钟,否则对话框PictureBox图片将被加载得太快,而我看不到它们。

So I tried to do that, and here is how the code looks: 因此,我尝试这样做,代码如下所示:

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
}

In my attempt of debugging this, I realised that if I use a MessageBox , the function will load the both photos. 在尝试调试此功能时,我意识到如果使用MessageBox ,该函数将加载两张照片。 Also I'm sure of the fact that the parameters will pass the if statement. 我也确信参数将通过if语句。

It seems so easy to fix this error, but I can't figure out how to do it. 解决这个错误似乎很容易,但是我不知道如何解决。

You probably need to issue a PictureBox.Refresh and/or a DoEvents command for the picture box to actually get a chance to load and display the picture. 您可能需要为图片框发出PictureBox.Refresh和/或DoEvents命令,才能真正获得加载和显示图片的机会。

The MessageBox automatically performs a DoEvents ... which is why it is working during debugging. MessageBox自动执行DoEvents ...,这就是为什么它在调试期间可以正常工作的原因。

What you're doing now just freezes the UI. 您现在正在执行的操作只是冻结UI。 Use a System.Windows.Forms.Timer instead. 请改用System.Windows.Forms.Timer Drop a Timer from the toolbox onto your Form. 将计时器从工具箱拖放到窗体上。

Then create some fields that the Timer can access, to store your pics and the current pic position: 然后创建一些计时器可以访问的字段,以存储您的图片和当前图片位置:

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

Finally, load it up with the pics you want to display, and start the Timer to go through them: 最后,用您要显示的图片加载它,然后启动计时器进行浏览:

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

Then you'll have to tell your Timer to display the next picture. 然后,您必须告诉计时器显示下一张图片。 Increase the counter, and reset it when necessary. 增加计数器,并在必要时将其重置。 Something like this should work. 这样的事情应该起作用。 Modify as necessary. 根据需要进行修改。

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