如何在 C# 中创建一个存储 PictureBox 名称的数组

How to create an array storing PictureBox names in C#

我想创建一个图片框名称数组,稍后我将在 FOREACH 循环中使用它并对每个图片框执行特定操作,而不必分别为所有图片框编写代码。在另一个解决方案中,下面的代码有效,但这里只有 returns 一个错误:"A field initializer cannot reference the non-static field, method, or property 'Puzzle.Form1.pic1'"

我无法在互联网上找到我要找的东西,尽管我尝试用不同的方式来写它。谢谢!

    //Define an array and place the 9 picture boxes in it

    //System.Drawing. [] PictureBox = {}
    //PictureBox[] Pictures1 = new PictureBox[9];
    //PictureBox[] Pictures1 = { pic1, pic2 };
    //PictureBox pBoxes = new PictureBox[] {pic1, pic2, pic3};

    PictureBox[] diceloc = { pic1, pic2, pic3, pic4, pic5, pic6, pic7, pic8, pic9 };

您需要先创建 PictureBox 的新实例 class,然后才能设置其值。

PictureBox[] diceloc = new PictureBox[] {pic1, pic2, pic3, pic4};