Stavros Afxentis
Stavros Afxentis

Reputation: 449

programmatically create panel and add picture boxes

I want to programmatically create a panel and add some pictureBoxes where i select the image through a for loop. I tried lots of ways but the form shows empty. My code is :

private void draw_pipeline()
    {

        Panel pnl = new Panel();

        pnl.Size = new System.Drawing.Size(1130, 145);
        pnl.Location = new Point(380, 260);
        pnl.BorderStyle = BorderStyle.FixedSingle;
        for (int i =0; i<3; i++)
        {

            PictureBox pic = new PictureBox();
            pic.SizeMode = PictureBoxSizeMode.Zoom;

            switch (i)
            {
                case 0: 
                    { 
                    pic.Location = new Point(3, 15); 
                    pic.Size = new Size(73, 121);
                    pic.Image = new Bitmap("if.png"); break;
                    }
                case 1:
                    {
                        pic.Location = new Point(76, 15); 
                        pic.Size = new Size(73, 121);
                        pic.Image = new Bitmap("line.png"); break;
                    }
            }
            pnl.Controls.Add(pic);
        }


    }

the result i want to create is illustrated in the picture below, that contains two picture boxes with two images, if.png which is the if-box image and the line.png which is the line image. I repeat the result of my code is that form shows empty!! Any help?

enter image description here

Upvotes: 0

Views: 8185

Answers (1)

adv12
adv12

Reputation: 8551

You'll need to add the Panel to the Form at some point, in the same way you're adding PictureBoxes to the Panel:

this.Controls.Add(pnl);

(The this is assuming that your draw_pipeline method belongs to the Form to which you're trying to add the Panel.)

Upvotes: 4

Related Questions