Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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# ImageList在ImageList.RemoveAt()处引发我出边界异常_C# - Fatal编程技术网

C# ImageList在ImageList.RemoveAt()处引发我出边界异常

C# ImageList在ImageList.RemoveAt()处引发我出边界异常,c#,C#,所以我想用C#在WinForms中制作一个匹配的游戏。我查看了这个MSDN项目:。我用imagelist替换了图标列表。根据这篇文章,我的图片应该在每张图片上显示两次。但每当它再次击中同一个数字时,它就会说它出界了,这就是我认为正在发生的事情。这是我的密码: public frmMain() { InitializeComponent(); imageInit(); imgToLbl(); } Random rnd

所以我想用C#在WinForms中制作一个匹配的游戏。我查看了这个MSDN项目:。我用imagelist替换了图标列表。根据这篇文章,我的图片应该在每张图片上显示两次。但每当它再次击中同一个数字时,它就会说它出界了,这就是我认为正在发生的事情。这是我的密码:

    public frmMain()
    {
        InitializeComponent();
        imageInit();
        imgToLbl();
    }

    Random rndImage = new Random();

    ImageList images = new ImageList();

    //list of file names
    List<string> files = new List<string>()
    {
        "Bavaria", "Denemarken", "Engeland",
        "Frankrijk", "Nederland", "oostenrijk",
        "Polen", "Pruissen", "Rusland", "Schotland",
        "Spanje", "Zweden"
    };

    // Method to put the files into the imagelist
    private void imageInit()
    {
        for(int i = 0; i < 12; i++)
        {
            images.Images.Add(files[i], Image.FromFile("../../images/" + files[i] + ".png"));
        }
    }

    // method to assign the images to a label in my form
    private void imgToLbl()
    {
        foreach (Control ctrl in tableLayoutPanel1.Controls)
        {
            Label imgLbl = ctrl as Label;
            if (imgLbl != null)
            {
                int rndNum = rndImage.Next(images.Images.Count);
                images.ImageSize = imgLbl.Size;
                imgLbl.ImageList = images;
                imgLbl.ImageIndex = rndNum;
                imgLbl.ImageList.Images.RemoveAt(rndNum);// this is where the exception is being thrown
            }
        }
    }
public frmMain()
{
初始化组件();
imageInit();
imgToLbl();
}
Random rndImage=新的Random();
ImageList images=新的ImageList();
//文件名列表
列表文件=新列表()
{
“巴伐利亚”、“德内马克”、“恩格兰”,
“弗兰克瑞克”、“荷兰”、“奥斯滕瑞克”,
“波伦”、“普鲁森”、“罗斯兰”、“肖特兰”,
“Spanje”、“Zweden”
};
//方法将文件放入imagelist中
私有void imageInit()
{
对于(int i=0;i<12;i++)
{
images.images.Add(文件[i],Image.FromFile(“../../images/”+文件[i]+”.png”);
}
}
//方法将图像指定给表单中的标签
私有无效imgToLbl()
{
foreach(tableLayoutPanel1.控件中的控件ctrl)
{
标签imgLbl=ctrl作为标签;
if(imgLbl!=null)
{
int rndNum=rndImage.Next(images.images.Count);
images.ImageSize=imgLbl.Size;
imgLbl.ImageList=图像;
imgLbl.ImageIndex=rndNum;
imgLbl.ImageList.Images.RemoveAt(rndNum);//这是引发异常的地方
}
}
}
以下是完全例外:

System.Windows.Forms.dll中发生类型为“System.ArgumentOutOfRangeException”的未处理异常

其他信息:InvalidArgument=值“8”对“索引”无效

我觉得这应该很容易,但我不知道如何修复这个异常。还有一件事。颜色都乱了,我不知道为什么

这是原件:

下面是应用程序中显示的混乱的一个:

有人能帮我吗?

  • 您正在销毁
    图像列表
    中的图像,但仍然需要
    标签
    列表项
    等。。参考他们。这行不通
只要任何控件或其他项需要显示它们,就需要将它们保留在周围

您可以看到,您只分配了一个数字作为
ImageIndex
。这个数字指向
图像列表
,因此图像仍然需要在那里

  • 另外:质量受您选择的
    图像的限制。ColorDepth
    。将其设置为深度32位!默认值仅为
    Depth8Bit

看起来您正试图从刚刚分配给它的标签图像列表中删除图像。最终,ImageList中会有0个条目,标签仍然引用索引。imgLbl.ImageList的分配是对已经存在的ImageList对象的引用,它不是克隆它,因此,当您从其列表中删除条目时,它是为已分配给实例的每个标签删除它。您希望克隆列表(或者更好)保留1个完整列表,并确保不重复选择相同的数字。

rndNum和ImageList的值是多少。图像长度?可以是0到12之间的任意值。当我运行它时,它会发生变化,因此我创建的随机数应该是0到11,但它应该仍然在边界内而不是在边界外如果你有两个问题,问两个问题