隐藏数组图片框 c#

本文关键字:数组 隐藏 | 更新日期: 2023-09-27 18:33:14

这会生成一个图片框

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

这是我得到的错误

错误:APPD 分配 2 中发生类型为"System.NullReferenceException"的第一次机会异常.exe(其他信息:对象引用未设置为对象的实例。

隐藏数组图片框 c#

你在这里没有使用错误的长度吗?

for (int i = 0; i < picturebox.Length; i++)

应该是

for (int i = 0; i < picturebox[0].Length; i++)

加载图片框时,仅从索引 13 开始,因此需要从 13 开始隐藏循环,或者检查 null。

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