从列表中分配时ArgumentOutOfRangeException

时间:2015-03-24 17:27:54

标签: c# list random picturebox

所以基本上在改变了我的一个老问题,例如2000万次之后,我终于找到了如何将我的两个变量分开并将它们匹配在一起,或者至少我认为我有。

我的代码基本上取自1和2的列表,然后根据是否分配了一个或两个来决定,如果玩家获得描绘某些内容的图片或描绘文本的图片,则相应地选择它们从列表和标记。

然而,当我为AssigningPicturesToPictureBoxes()运行我的方法时,抛出了ArgumentOutOfRangeException并且我无法理解为什么,因为每当调用整数列表图片时它都不会发生,但每当整数列表时它确实会引起问题文本被称为。由于这两组代码实际上是相同的,我无法理解我的生活中有什么问题。如果有人能帮助我,我真的很感激。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace BinaryFileReader
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            AssignTorPToPictureBoxes();

            AssignPicturesToPictureBoxes();
        }

        Random random = new Random();

            List<int> pictures = new List<int>() 
            { 
                1,2,3,4,5,6,7,8
            };

            List<int> texts = new List<int>() 
            { 
                1,2,3,4,5,6,7,8
            };

            List<int> TorP = new List<int>()
            {
                1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2
            };

            private void AssignPicturesToPictureBoxes()
            {
                foreach(Control Control in tableLayoutPanel1.Controls)

                {
                    PictureBox picturebox = Control as PictureBox;

                    if (picturebox.Tag.ToString() == "1")
                    {
                        int randomNumber = random.Next(pictures.Count);

                        int unconvertedtag = pictures[randomNumber];

                        string convertedtag = unconvertedtag.ToString();

                        picturebox.Tag = convertedtag;

                        picturebox.Visible = false;

                        pictures.RemoveAt(randomNumber);

                        if (picturebox.Tag.ToString() == "1")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLSIGNA;
                        }

                        if (picturebox.Tag.ToString() == "2")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLSIGNBREAKFAST;
                        }

                        if (picturebox.Tag.ToString() == "3")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLSIGNCAR;
                        }

                        if (picturebox.Tag.ToString() == "4")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLSIGNCAT;
                        }

                        if (picturebox.Tag.ToString() == "5")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLSIGNCOFFEE;
                        }

                        if (picturebox.Tag.ToString() == "6")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLSIGNDOG;
                        }

                        if (picturebox.Tag.ToString() == "7")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLSIGNENGLAND;
                        }

                        if (picturebox.Tag.ToString() == "8")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLSIGNH;
                        }

                    }

                    if (picturebox.Tag.ToString() == "2")
                    {
                        int randomNumber = random.Next(texts.Count);

                        int unconvertedtag = texts[randomNumber];

                        string convertedtag = unconvertedtag.ToString();

                        picturebox.Tag = convertedtag;

                        picturebox.Visible = false;

                        texts.RemoveAt(randomNumber);

                        if (picturebox.Tag.ToString() == "1")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLTEXTA;
                        }

                        if (picturebox.Tag.ToString() == "2")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLTEXTBREAKFAST;
                        }

                        if (picturebox.Tag.ToString() == "3")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLTEXTCAR;
                        }

                        if (picturebox.Tag.ToString() == "4")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLTEXTCAT;
                        }

                        if (picturebox.Tag.ToString() == "5")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLTEXTCOFEE;
                        }

                        if (picturebox.Tag.ToString() == "6")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLTEXTDOG;
                        }

                        if (picturebox.Tag.ToString() == "7")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLTEXTENGLAND;
                        }

                        if (picturebox.Tag.ToString() == "8")
                        {
                            picturebox.Image = global::BinaryFileReader.Properties.Resources.BSLTEXTH;
                        }
                    }



                }

            }

            private void AssignTorPToPictureBoxes()
            {
                foreach (Control Control in tableLayoutPanel1.Controls)
                {
                    PictureBox picturebox = Control as PictureBox;
                    if (picturebox != null)
                    {
                        int randomNumber = random.Next(TorP.Count);
                        int unconvertedtag = TorP[randomNumber];
                        string convertedtag = unconvertedtag.ToString();
                        picturebox.Tag = convertedtag;
                        TorP.RemoveAt(randomNumber);

                    }
                }      
            }

            private void pictureBox1_Click(object sender, EventArgs e)
            {
                PictureBox picturebox = sender as PictureBox;

                if (picturebox != null)
                {

                    if (picturebox.Visible == true)
                    {
                        return;
                    }

                    else
                    {
                        picturebox.Visible = true;
                    }
                }
            }
    }
}

在旁注中,任何人都想知道为什么我这样做的原则,这是一个游戏,其中玩家点击一个图片框,他们点击另一个图片框并尝试将它们匹配在一起在一个时间限制内,很遗憾,这是该计划目前唯一没有工作的部分。

编辑:对不起,我不够具体,我只会在一秒钟内获得特定部分

EDIT2:  错误特别出现在

部分

int unsvertedtag = text [randomNumber];

Random random = new Random();

        List<int> pictures = new List<int>() 
        { 
            1,2,3,4,5,6,7,8
        };

        List<int> texts = new List<int>() 
        { 
            1,2,3,4,5,6,7,8
        };

        List<int> TorP = new List<int>()
        {
            1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2
        };

        private void AssignPicturesToPictureBoxes()
        {
            foreach(Control Control in tableLayoutPanel1.Controls)

            {
                PictureBox picturebox = Control as PictureBox;

                if (picturebox.Tag.ToString() == "1")
                {
                    int randomNumber = random.Next(pictures.Count);

                    int unconvertedtag = pictures[randomNumber];

                    string convertedtag = unconvertedtag.ToString();

                    picturebox.Tag = convertedtag;

                    picturebox.Visible = false;

                    pictures.RemoveAt(randomNumber);
                }

                if (picturebox.Tag.ToString() == "2")
                {
                    int randomNumber = random.Next(texts.Count);

                    int unconvertedtag = texts[randomNumber];

                    string convertedtag = unconvertedtag.ToString();

                    picturebox.Tag = convertedtag;

                    picturebox.Visible = false;

                    texts.RemoveAt(randomNumber);
                }

0 个答案:

没有答案