C# 从列表分配时ArgumentOutOfRange异常
所以基本上,在对我的一个老问题进行了2000万次的修改之后,我终于找到了如何分离两个变量并将它们匹配在一起的方法,或者至少我认为我找到了 我的代码基本上是从1和2的列表中提取,然后根据是否分配了1或2来决定,玩家应该得到一张描述某物的图片还是一张描述文本的图片,并相应地从列表中选择并标记它们 但是,当我运行分配PictureStoPictureBoxes()的方法时,会抛出ArgumentOutOfRangeException,我无法理解原因,因为它不会在调用整数列表图片时发生,但在调用整数列表文本时确实会导致问题。由于这两套代码实际上是完全相同的,我无法理解它们到底出了什么问题。如果有人能帮我,我会非常感激的C# 从列表分配时ArgumentOutOfRange异常,c#,list,random,picturebox,C#,List,Random,Picturebox,所以基本上,在对我的一个老问题进行了2000万次的修改之后,我终于找到了如何分离两个变量并将它们匹配在一起的方法,或者至少我认为我找到了 我的代码基本上是从1和2的列表中提取,然后根据是否分配了1或2来决定,玩家应该得到一张描述某物的图片还是一张描述文本的图片,并相应地从列表中选择并标记它们 但是,当我运行分配PictureStoPictureBoxes()的方法时,会抛出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;
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
命名空间二进制文件读取器
{
公共部分类表单2:表单
{
公共表格2()
{
初始化组件();
赋值函数()的;
AssignPicturesToPictureBoxes();
}
随机=新随机();
列表图片=新列表()
{
1,2,3,4,5,6,7,8
};
列表文本=新列表()
{
1,2,3,4,5,6,7,8
};
List TorP=新列表()
{
1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2
};
私有void AssignPicturesToPictureBoxes()的
{
foreach(tableLayoutPanel1.控件中的控件)
{
PictureBox PictureBox=控件作为PictureBox;
如果(picturebox.Tag.ToString()=“1”)
{
int randomNumber=random.Next(pictures.Count);
int unconfertedtag=图片[随机数];
字符串convertedtag=unvertedtag.ToString();
picturebox.Tag=convertedtag;
picturebox.Visible=假;
图片。删除(随机编号);
如果(picturebox.Tag.ToString()=“1”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.BSLSIGNA;
}
if(picturebox.Tag.ToString()=“2”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.BSLSignBreaken;
}
如果(picturebox.Tag.ToString()=“3”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.BSLSIGNCAR;
}
如果(picturebox.Tag.ToString()=“4”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.BSLSIGNCAT;
}
如果(picturebox.Tag.ToString()=“5”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.BSLSIGNCOFFEE;
}
如果(picturebox.Tag.ToString()=“6”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.BSLSIGNDOG;
}
如果(picturebox.Tag.ToString()=“7”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.bslsigneland;
}
如果(picturebox.Tag.ToString()=“8”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.BSLSIGNH;
}
}
if(picturebox.Tag.ToString()=“2”)
{
int randomNumber=random.Next(text.Count);
int unconfertedtag=文本[随机数];
字符串convertedtag=unvertedtag.ToString();
picturebox.Tag=convertedtag;
picturebox.Visible=假;
text.RemoveAt(随机数);
如果(picturebox.Tag.ToString()=“1”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.BSLTEXTA;
}
if(picturebox.Tag.ToString()=“2”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.bsltextbreaken;
}
如果(picturebox.Tag.ToString()=“3”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.BSLTEXTCAR;
}
如果(picturebox.Tag.ToString()=“4”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.BSLTEXTCAT;
}
如果(picturebox.Tag.ToString()=“5”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resources.BSLTEXTCOFEE;
}
如果(picturebox.Tag.ToString()=“6”)
{
picturebox.Image=global::BinaryFileReader.Properties.Resource
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);
}