【问题标题】:how to add picture box to the <list> dynamically如何将图片框动态添加到 <list>
【发布时间】:2012-09-07 11:26:32
【问题描述】:

我想添加 23 个我一直在表单中使用的图片框。我试过这种方法,但没有用:

List<PictureBox> pbList = new List<PictureBox>();

for (int i = 0; i < 23; i++)
  pbList.Add(pictureBox +i);  

【问题讨论】:

  • 小心使用PictureBox控件,如果处理不当会导致memory leaks

标签: c# list picturebox


【解决方案1】:

您正在尝试按名称添加它们,您必须进行查找:

List<PictureBox> pbList = new List<PictureBox>();

for (int i = 0; i < 23; i++)
  pbList.Add((PictureBox)Controls.Find("pictureBox" + i)); 

编辑:如果您的图片框嵌套在其他控件中,则必须将“true”传递给 Find() 方法,以便搜索所有子控件。其他解决方案(包括我的)将仅搜索直接在控件容器“控件”中的图片框

【讨论】:

  • 我有问题 ıt 说 = 方法 'find' 没有重载需要 1 个 orguments。对不起,我的编程知识很差,但我找不到问题
【解决方案2】:
var pbList = Controls.OfType<PictureBox>().Select(a => a.Name.StartsWith("pictureBox"));

【讨论】:

    猜你喜欢
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2019-03-18
    • 1970-01-01
    • 2011-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多