动态添加所有图片框到列表?(Dynamically add all pictureboxes to list?)
我正在尝试创建一个名为“pictureBox”+ x的所有图片框的列表。 我尝试了一些我在网上找到的不同方法,但它们没有用:(
简单的“代码”:
int x = 1; List<PictureBox> list= new List<PictureBox>(); if (there is a picturebox named "pictureBox" + x) { list.Add("pictureBox" + x as type PictureBox); x = x + 1; } else Stop running this code;所以x将会增加,直到找不到更多的图片框。 我怎样才能做到这一点? 谢谢 :)
编辑:我在visual studio中使用winform,我试图使用
list.Add((PictureBox)Controls.Find("pictureBox" + x));但我无法让它发挥作用。
I'm trying to create a list of all pictureboxes named "pictureBox" + x. I've tried some different methods I found online, but they didn't work :(
Simple "code":
int x = 1; List<PictureBox> list= new List<PictureBox>(); if (there is a picturebox named "pictureBox" + x) { list.Add("pictureBox" + x as type PictureBox); x = x + 1; } else Stop running this code;So x will be increasing until there is no more pictureboxes found. How can I do this? Thanks :)
Edit: I'm using a winform in visual studio and I tried to use
list.Add((PictureBox)Controls.Find("pictureBox" + x));but I can't get that to work.
最满意答案
尝试这个,
foreach(var pb in this.Controls.OfType<PictureBox>()) { //do stuff }这将为您提供PictureBoxes。 并在foreach循环中检查其名称。 如果符合您的条件,请添加列表。
Try this,
foreach(var pb in this.Controls.OfType<PictureBox>()) { //do stuff }This will give you the PictureBoxes. And check its name inside the foreach loop. Add the list if it fits your conditions.
更多推荐
发布评论