动态添加所有图片框到列表?(Dynamically add all pictureboxes to list?)

编程入门 行业动态 更新时间:2024-10-26 21:19:00
动态添加所有图片框到列表?(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.

更多推荐

本文发布于:2023-07-28 04:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1300356.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动态   列表   图片   Dynamically   pictureboxes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!