Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 避免同一图像在随机显示时同时显示多个图像_C#_.net - Fatal编程技术网

C# 避免同一图像在随机显示时同时显示多个图像

C# 避免同一图像在随机显示时同时显示多个图像,c#,.net,C#,.net,我使用此代码使用计时器一次在3个图片框中随机显示图像,现在我用文件夹中的5个图像对其进行测试,最初它在3个图片框中正确显示不同的图像,但后来相同的图像出现在2个图片框中,甚至其中3个显示相同的图像,如何避免在多个图片框中同时显示同一图像 public partial class Form2 : Form { Random random = new Random(); List<String> filesToShow = new List<

我使用此代码使用计时器一次在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();
            }

        }
}
公共部分类表单2:表单
{
随机=新随机();
List filesToShow=新列表();
列出图片框;
字符串登录名;
公共表单2(字符串用户名)
{
初始化组件();
this.LoginName=用户名;
label1.Text=“欢迎”+用户名;
timer2=新定时器();
pictureBoxes=新列表{
图3,
图4,
图片框x5
};
//设置计时器
timer2.Interval=5*1000;//1000ms=1秒
timer2.Tick+=新事件处理程序(timer2\u Tick);
timer2.Start();
panelHide=panel4;
}
公共静态面板panelHide=新面板();
私有图像()
{
foreach(pictureBox中的var pictureBox)
{
if(filesToShow!=null&&!filesToShow.Any())
{
filesToShow=GetFilesToShow();
}
if(filesToShow!=null&&filesToShow.Any())//如果有文件,则允许代码删除显示的图像
{
int index=random.Next(0,filesToShow.Count);
字符串fileToShow=filesToShow[index];
pictureBox.ImageLocation=filesToShow[index];
filesToShow.RemoveAt(索引);
}
}
}
私有列表GetFilesToShow()
{
字符串str=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutionGassembly().Location);
字符串路径=str+“\\Image\\”;
返回Directory.GetFiles(路径“*.jpg”,SearchOption.TopDirectoryOnly).ToList();
}
私有无效计时器2_刻度(对象发送方,事件参数e)
{
如果(发送方==定时器2)
{
显示随机图像();
}
}
}

提前感谢您的帮助

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

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);
        }
    }
}
private void ShowRandomImages()
{
List selectedImages=新建列表();
foreach(pictureBox中的var pictureBox)
{
if(filesToShow!=null&&!filesToShow.Any())
{
filesToShow=GetFilesToShow();
}
if(filesToShow!=null&&filesToShow.Any())//如果有文件,则允许代码删除显示的图像
{
int指数=-1;
如果(filesToShow.Count>=pictureBoxes.Count)
{
bool-bOk=false;
而(!bOk)
{ 
index=random.Next(0,filesToShow.Count);
bOk=所选图像。索引of(index)=-1;
}
}
其他的
{
index=random.Next(0,filesToShow.Count);
}
选择图像。添加(索引);
字符串fileToShow=filesToShow[index];
pictureBox.ImageLocation=filesToShow[index];
filesToShow.RemoveAt(索引);
}
}
}

希望能有所帮助。

当然,当您在
文件中显示最后一幅图像时,会出现错误,显示列表将变为空,从而导致执行

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

之后,您的
文件显示所有以前删除的图像,以便可以再次显示。

Random
controlling flow
两者都是矛盾的。重新设计算法。尝试将随机化、文件IO、计时、显示和将当前项目排除到独立方法中的问题隔离开来。问题之一是所有的东西都是一堆乱七八糟的代码。我开始回答这些问题,但问题结束了。它向我显示了以下错误:
与“System.Collections.Generic.List.Find(System.Predicate)”匹配的最佳重载方法具有一些无效参数
我已更新了代码以修复此错误。我不知道为什么,但它正在接近该值的位置循环,而循环仅用于索引0到5,并且图像未显示,我没有得到它。很抱歉,我已将bOk检查写错了。我已经纠正了。我试过多次重新拍摄和显示静止图像