我想要做的就是将我的图片框保存在一个数组中以便稍后处理它们, 我做了一个解决方案,以获得这些图片框,但它太大了,我非常肯定这是一种更轻松的方式,这就是我想要的
public PictureBox[] getboxes()
{
PictureBox[] boxes = new PictureBox[]{pictureBox1,pictureBox2,pictureBox3,pictureBox4,pictureBox5,pictureBox6,pictureBox7,pictureBox8,pictureBox9,pictureBox10,pictureBox20,pictureBox19,pictureBox18,pictureBox17,pictureBox16,pictureBox15,pictureBox14,pictureBox13,pictureBox12,pictureBox11,
pictureBox40,pictureBox39,pictureBox38,pictureBox37,pictureBox36,pictureBox35,pictureBox34,pictureBox33,pictureBox32,pictureBox31,pictureBox30,pictureBox29,pictureBox28,pictureBox27,pictureBox26,pictureBox25,pictureBox24,pictureBox23,pictureBox22,pictureBox21,
pictureBox60,pictureBox59,pictureBox58,pictureBox57,pictureBox56,pictureBox55,pictureBox54,pictureBox53,pictureBox52,pictureBox51,pictureBox70,pictureBox69,pictureBox68,pictureBox67,pictureBox66,pictureBox65,pictureBox64,pictureBox63,pictureBox62,pictureBox61,
pictureBox80,pictureBox79,pictureBox78,pictureBox77,pictureBox76,pictureBox75,pictureBox74,pictureBox73,pictureBox72,pictureBox71,pictureBox50,pictureBox49,pictureBox48,pictureBox47,pictureBox46,pictureBox45,pictureBox44,pictureBox43,pictureBox42,pictureBox41,
pictureBox90,pictureBox89,pictureBox88,pictureBox87,pictureBox86,pictureBox85,pictureBox84,pictureBox83,pictureBox82,pictureBox81,pictureBox100,pictureBox99,pictureBox98,pictureBox97,pictureBox96,pictureBox95,pictureBox94,pictureBox93,pictureBox92,pictureBox91};
return boxes;
}
你可以看到这太大了
是否有任何帮助使这更容易?
答案 0 :(得分:1)
每个.Net控件都有一个属性Controls,允许访问其子级。
因此,如果您的所有图片框都放在同一个容器中,让我们说一个Panel“panel1”来检索您可以使用的每个图片框:
foreach(var ctl in panel1.Controls)
{
var box = ctl as PictureBox;
if(box != null)
{
// Do what you want : add to an array, get the picture...
}
}
用所有图片框填充数组:
List<PictureBox> pboxes = new List<PictureBox>();
foreach(var ctl in panel1.Controls)
{
var box = ctl as PictureBox;
if(box != null)
{
pboxes.Add(box);
}
}
// pboxes now contains all PB. You can call "AsArray()" LINQ method on it to get an array rather than a List.