将64个图片框分组到picturebox数组中

本文关键字:picturebox 数组 64个 | 更新日期: 2023-09-27 18:01:30

我使用GUI创建了64个图片框。现在我想将这些图片框分组到一个picturebox数组中,因为我需要它不断地更新上述图片框中的图片。

我有以下代码:

private PictureBox[] pictureBoxArray= new PictureBox[64]; //Initialize array to group picture boxes into picture box array
  private void Main_Load(object sender, EventArgs e)
    {
        ConvertGuiPBtoGuiPbArray(ref pictureBoxArray);
    }
public static void ConvertGuiPBtoGuiPbArray(ref PictureBox[] pictureBoxArray)
    {
        foreach(PictureBox index in pictureBoxArray)
        {
        //Some code to do the following:
        pictureBoxArray[0]=pictureBox1; //this is the name of the pictureBox on the GUI
        pictureBoxArray[1]=pictureBox2;
        pictureBoxArray[2]=pictureBox3;
        .
        .
        .
        pictureBoxArray[63]=pictureBox64;
       //
    }

我看到了控件的命令。但我似乎不理解它。任何建议都将非常感谢。提前感谢您的帮助!

将64个图片框分组到picturebox数组中

假设您坚持表单的设计和64个单独的pictureBoxNN属性,基本反射可以根据需要填充数组。我认为Controls不能保证有正确的顺序。

for (int i = 0; i < 64; i++)
{
    pictureBoxArray[i] = (PictureBox)GetType().GetProperty("pictureBox" + (i + 1)).GetValue(this);
}

这段代码将窗体的所有图片框添加到您的pictureBoxArray

private PictureBox[] pictureBoxArray;
private void Main_Load(object sender, EventArgs e)
{
     pictureBoxArray = this.Controls.OfType<PictureBox>().OrderBy(x => x.Name).ToArray();
}

"this"指的是Form,如果你想添加其他容器的图片框,如groupbox任何容器只需替换"this"容器的名称

如果你想按降序排序,你可以使用 orderbydescent 而不是OrderBy

完整的代码可以像这样:

private static PictureBox[] pictureBoxArray;
public static void ConvertGuiPBtoGuiPbArray(ContainerControl container)
{
    pictureBoxArray = container.Controls.OfType<PictureBox>().OrderBy(x => x.Name).ToArray();
}
private void Main_Load(object sender, EventArgs e)
{
    ConvertGuiPBtoGuiPbArray(this);//Or Whatever container like groupbox
}

如果它们没有嵌套在其他控件中,而是直接放置在窗体上,则可以这样做:

PictureBox[] pictureBoxes = this.Controls.OfType<PictureBox>().ToArray();
编辑:

对于嵌套控件,您需要遍历所有控件容器并检查它们是否包含PictureBox。你可以这样做:

private void Form_Shown(object sender, EventArgs e)
{
    List<PictureBox> pictureBoxes = new List<PictureBox>();
    GetAllPictureBoxes(ref pictureBoxes, this.Controls);
    PictureBox[] pictureBoxesArray = pictureBoxes.OrderBy(pb => pb.Name).ToArray();
}
private void GetAllPictureBoxes(ref List<PictureBox> pictureBoxes, Control.ControlCollection controls)
{
    foreach (Control control in controls)
    {
        if (control.HasChildren)
            GetAllPictureBoxes(ref pictureBoxes, control.Controls);
        if (control is PictureBox)
            pictureBoxes.Add((PictureBox)control);
    }
}

过了一段时间,我开始使用以下方法:

private PictureBox[] pictureBoxArray = new PictureBox[64]; //Initialize array to group picture boxes into picture box array
private void Main_Load(object sender, EventArgs e)
    {
        ConvertGuiPBtoGuiPbArray(ref pictureBoxArray);
    }
public void ConvertGuiPBtoGuiPbArray(ref PictureBox[] pictureBoxArray)
{
        int i = 0;
        string NameofPictureBox;
        string PictureBoxNumber;
        foreach (var item in groupBox_DataLayer.Controls)
        {
            if (item is PictureBox)
            {
                NameofPictureBox = ((PictureBox)item).Name;
                PictureBoxNumber = Regex.Match(NameofPictureBox, @"'d+").Value; // 'd+ is regex for integers only
                i = Int32.Parse(PictureBoxNumber);  //Convert only number string to int
                i = i - 1;  //bcs of index of array starts from 0-63, not 1-64
                pictureBoxArray[i] = (PictureBox)item; //put PictureBox object in desired position
            }
        }
}

这完全适用于我,我有排序的问题,但现在一切都在工作。谢谢大家的帮助!

您可以使用LinQ查询来搜索具有特定名称的控件:

public void ConvertGuiPBtoGuiPbArray(ref PictureBox[] pictureBoxArray)
{
    for (int i = 0; i <= pictureBoxArray.Count() - 1; i++)
    {
        int count = i;
        pictureBoxArray[i] = (PictureBox)this.Controls.Cast<Control>().Where(c => c.Name.ToLower() == "picturebox" + (count + 1)).First();
    }
}

你基本上循环从0到63的索引,并搜索名称为"picturebox + (index+1)"的picturebox,并将其分配给数组中的这个索引。

如果源图片框未在this.Controls集合中排序,则跳过问题。

考虑到您对分组框的评论:在这种情况下,图片框不是this.Controls的一部分,您需要用

替换查询
pictureBoxArray[i] = (PictureBox)groupboxName.Controls.Cast<Control>().Where(c => c.Name.ToLower() == "picturebox" + (count + 1)).First();