【问题标题】:Hiding an array picturebox c#隐藏数组图片框c#
【发布时间】:2016-02-04 10:23:13
【问题描述】:

这会生成一个图片框

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

我试图隐藏一个锯齿状数组,它是 winform c# 中的图片框,但我不断收到错误消息,是否可以隐藏锯齿状数组?这是我遇到问题的代码。

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

这是我得到的错误

错误:APPD 分配 2.exe 中出现“System.NullReferenceException”类型的第一次机会异常(附加信息:对象引用未设置为对象的实例。)

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    你不是在这里用错了长度吗?

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

    【讨论】:

    • 好点,但我认为他的第二个数组维度的索引也从 13 开始,所以仍然认为您可能需要从 13 开始 i 或空检查。
    • 我试着改成那个,但还是有错误:c错误指向Hide();部分代码
    • 对不起,那个 13 是用来测试的,应该是从 0 开始的,我的错! ''>.
    • 非常感谢!这是第 13 部分,哈哈,我想我需要休息一下:D
    猜你喜欢
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    相关资源
    最近更新 更多