0

我正在使用此代码使用计时器一次在 3 个图片框中随机显示图像,现在我在我的文件夹中使用 5 个图像对其进行测试,最初它在 3 个图片框中正确显示不同的图像,但后来出现相同的图像2个图片框甚至3个图片框显示相同的图像,如何避免在多个图片框中同时显示相同的图像

public partial class Form2 : Form
    {
        Random random = new Random();
        List<String> filesToShow = new List<string>();
        List<PictureBox> pictureBoxes;
        string LoginName;
        public Form2(string userName)
        {
            InitializeComponent();
            this.LoginName = userName;
            label1.Text = "Welcome " + userName;
            timer2 = new Timer();
            pictureBoxes = new List<PictureBox> {
            pictureBox3,
            pictureBox4,
            pictureBox5
        };
            // Setup timer
            timer2.Interval = 5 * 1000; //1000ms = 1sec
            timer2.Tick += new EventHandler(timer2_Tick);
            timer2.Start();
            panelHide = panel4;

        }
        public static Panel panelHide = new Panel();

        private void ShowRandomImages()
        {
            foreach (var pictureBox in pictureBoxes)
            {
                if (filesToShow != null && !filesToShow.Any())
                {
                    filesToShow = GetFilesToShow();
                }

                if (filesToShow != null && filesToShow.Any()) // If any files then allow the code to delete the shown images
                {
                    int index = random.Next(0, filesToShow.Count);
                    string fileToShow = filesToShow[index];
                    pictureBox.ImageLocation = filesToShow[index];
                    filesToShow.RemoveAt(index);
                }
            }
        }
        private List<String> GetFilesToShow()
        {
            String str = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string path = str + "\\Image\\";
            return Directory.GetFiles(path, "*.jpg", SearchOption.TopDirectoryOnly).ToList();
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (sender == timer2)
            {
             ShowRandomImages();
            }

        }
}

在此先感谢您的帮助

4

2 回答 2

1

一个非常简单的解决方案是创建一个包含所选图像的列表,并在显示新图像之前检查它们。

private void ShowRandomImages()
{
    List<int> selectedImages = new List<int>();
    foreach (var pictureBox in pictureBoxes)
    {
        if (filesToShow != null && !filesToShow.Any())
        {
            filesToShow = GetFilesToShow();
        }

        if (filesToShow != null && filesToShow.Any()) // If any files then allow the code to delete the shown images
        {
            int index = -1;
            if (filesToShow.Count >= pictureBoxes.Count)
            {
                bool bOk = false;
                while( !bOk )
                { 
                    index = random.Next(0, filesToShow.Count);
                    bOk = selectedImages.IndexOf(index) == -1;
                }
            }
            else
            {
                index = random.Next(0, filesToShow.Count);
            }

            selectedImages.Add(index);
            string fileToShow = filesToShow[index];
            pictureBox.ImageLocation = filesToShow[index];
            filesToShow.RemoveAt(index);
        }
    }
}

希望能帮助到你。

于 2013-08-18T07:43:32.940 回答
0

当然,当您在filesToShow列表中显示最后一个图像时会发生错误,因此列表变为空导致执行

if (filesToShow != null && !filesToShow.Any())
{
    filesToShow = GetFilesToShow();
}

之后,您filesToShow已经删除了所有以前的图像,因此可以再次显示它们。

于 2013-08-18T08:00:14.843 回答