mardi 24 mars 2015

ArgumentOutOfRangeException while assigning from a list

So basically after reforming one of my old questions like 20 million times, I've finally figured out how to separate two of my variables apart and match them together, or at least I thought I had.


My code basically takes from a list of 1s and 2s and then decides based on whether a one or a two was assigned, should the player get a picture that depicts something or a picture that depicts text, and accordingly, they are chosen from lists and tagged.


However when I run my method for AssigningPicturesToPictureBoxes(), an ArgumentOutOfRangeException is thrown and I cannot understand why, because it doesn't occur whenever the integer list pictures is called, but it does cause a problem whenever the integer list texts is called. As both sets of code are practically identical, I cannot understand for the life of me what is wrong with them. I would really appreciate if someone could help me with this.



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;
}
}
}
}
}


On a side note, incase anyone is wondering the principle behind why I'm doing this, this is for a game, wherein the player clicks on a picturebox, and they click on another picturebox and attempt to match them together within a time limit, this is unfortunately the only part of the program that isn't working at the moment.





Aucun commentaire:

Enregistrer un commentaire