这会生成一个图片框
PictureBox[][] picturebox;
public void loadPictureBox()
{
string path = @"../../Images/Catelogue/"; //set pathing
string[] list = Directory.GetFiles(path, "*.jpg");
//pictureboxCatelogue = new PictureBox[list.Length];
//pictureboxCosplay = new PictureBox[list.Length];
picturebox = new PictureBox[4][];
for (int i = 0; i < 4; i++)
{
picturebox[i] = new PictureBox[list.Length];
int y = 85, temp = 220, run = 0;
for (int index = 13; index < list.Length; index++) // loads all pictures and create pictureboxes
{
picturebox[i][index] = new PictureBox();
picturebox[i][index].Image = Image.FromFile(path + index + ".jpg");
this.Controls.Add(picturebox[i][index]);
temp = temp + 200;
if (index % 4 == 0)
{
if (run != 0)
y = y + 200;
run++;
temp = 220;
}
picturebox[i][index].Location = new Point(temp, y);
picturebox[i][index].Size = new Size(180, 180);
picturebox[i][index].Name = Convert.ToString(index);
picturebox[i][index].SizeMode = PictureBoxSizeMode.Zoom;
picturebox[i][index].BackColor = Color.FromArgb(35, 35, 35);
picturebox[i][index].Click += new System.EventHandler(PictureBox_Click);
}
}
}
我试图隐藏一个锯齿状阵列,这是winsform c#中的一个图片框,但是我一直收到错误,是否可能隐藏锯齿状阵列?这是我遇到问题的代码。
for (int i = 0; i < picturebox.Length; i++)
{
picturebox[0][i].Hide();
}
这是我得到的错误
错误:类型&#39; System.NullReferenceException&#39;的第一次机会异常。发生在APPD Assignment 2.exe中(附加信息:对象引用未设置为对象的实例。)
答案 0 :(得分:0)
Aren你在这里使用了错误的长度吗?
for (int i = 0; i < picturebox.Length; i++)
应该是
for (int i = 0; i < picturebox[0].Length; i++)
当您加载图片框时,您只需从索引13开始,因此您需要在13处启动隐藏循环,或检查是否为空。
for (int i = 13; i < picturebox[0].Length; i++)
{ ... }
或
for (int i = 0; i < picturebox[0].Length; i++)
{
if (picturebox[0][i] != null)
{
picturebox[0][i].Hide();
}
}