user4219996
user4219996

Reputation:

Adding pictureBox to panel with codes

I have a panel in Visual Studio/windows form app.But I coulnd't add pictureBox on it with code.It is working if I work without panel however I need it.MyCodes:

      PictureBox[] pipe = new PictureBox[3];

 private void Form1_Load(object sender, EventArgs e)
    {
        CreatePipes(1);}

    private void CreatetopPipes(int Number)
    {
          for (int i = 0; i <= Number; i++)
        {

            PictureBox temp = new PictureBox();
            this.Controls.Add(temp);
            temp.Width = 50;
            temp.Height = 350;
            temp.BorderStyle = BorderStyle.FixedSingle;
            temp.BackColor = Color.Red;
            temp.Top = 30;
            temp.Left = 300;
            topPipe[i] = temp;
            topPipe[i].Visible = true;


        }
    }

Upvotes: 1

Views: 6744

Answers (1)

Abdellah OUMGHAR
Abdellah OUMGHAR

Reputation: 3755

You can use panelName.Controls.Add(temp), and i advise you to change a top of PictureBox in for loop to view all PictureBox, like this :

private void CreatetopPipes(int Number)
{
    for (int i = 0; i <= Number; i++)
    {

        PictureBox temp = new PictureBox();
        panelName.Controls.Add(temp);
        temp.Width = 50;
        temp.Height = 350;
        temp.BorderStyle = BorderStyle.FixedSingle;
        temp.BackColor = Color.Red;
        temp.Top = temp.Height * panelName.Controls.Count;
        temp.Left = 300;
        topPipe[i] = temp;
        topPipe[i].Visible = true;

    }
}

Upvotes: 1

Related Questions