'ForEach Loop not detecting any Controls (PictureBoxes)

This following code is supposed to detect all PictureBoxes in the Controls property, and apply collisions to it. (This is in Windows Form for a CompSci final project). The problem is that it is not detecting any PictureBoxes; otherwise, the code runs. Any ideas?

foreach (Control x in this.Controls)
            {
                if (x is PictureBox)
                {
                    if ((string)x.Tag == "platform")
                    {
                        if (PlayerPB.Bounds.IntersectsWith(x.Bounds))
                        {
                            force = 8;
                            PlayerPB.Top = x.Top - PlayerPB.Height;
                        }

                        x.BringToFront();
                    }
                }
            }


Solution 1:[1]

You need to recurse into each container within the main form and see if there are any PictureBoxes within there:

private void WirePBs(Control container) 
{
    foreach (Control x in container.Controls)
    {
        if (x is PictureBox) && (string)x.Tag == "platform")
        {
            if (PlayerPB.Bounds.IntersectsWith(x.Bounds))
            {
                force = 8;
                PlayerPB.Top = x.Top - PlayerPB.Height;
            }
            x.BringToFront();
        }
        else if (x.HasChildren)
        {
             WirePBs(x);
        }
    }
}

Then you can call it by passing in the form itself:

WirePBs(this);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Idle_Mind