Search code examples
c#winformspicturebox

Putting picturebox on picturebox


I have a problem adding picture boxes using Controls class on another picture box that already exists.

when i load the form i don't see any changes because the "coded" pictures are under the main picture (Arena)

Here is my code:

void drawSpikes()
{
    PictureBox[] spikes = new PictureBox[Arena.Height / 25 * 2];
    int position = 0;
    byte wall = 1;
    byte spike_count = 0;

    for (int i = 0; i < Arena.Height / 25 * 2; i++)
    {
        spikes[i] = new PictureBox();
    }
    foreach (var Spike in spikes)
    {
        if (spike_count == 18) wall = 2;

        Spike.Size = new Size(25, 25);
        if (wall == 1)
        {
            Spike.Location = new Point(21, position);
            Spike.BackColor = Color.Yellow;
        }
        if (wall == 2)
        {
            Spike.Location = new Point(position, 250);
            Spike.BackColor = Color.Yellow;
        }

        if (position == 450) position = 0;
        position += 25;
        spike_count += 1;
        Controls.Add(Spike);
    }
}

How can i fix it ?

sorry for the function disconnection (i am new in stack overflow).


Solution

  • You can use the BringToFront() function after adding the picture box to the controls collection:

    Controls.Add(Spike);
    
    Spike.BringToFront();