简体   繁体   中英

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?

在此处输入图片说明

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 .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM