How to dynamically produce pictureboxes, via button click?

29,469

Solution 1

It turns out, the problem is that there was a picturebox acting as a background image for the program, that was almost as large as the winform itself. The picturebox blocked all controls that were programatically created, thus giving the illusion of the code doing nothing.

Solution 2

May this code help you to get concept : This is the one i'm working for creating picturebox,textbox and button dynamically in flowlayoutpanel.

   PictureBox[] pics = new PictureBox[50];
    TextBox[] txts = new TextBox[50];
    Button[] butns = new Button[50];
    FlowLayoutPanel[] flws = new FlowLayoutPanel[50]
    static int brh =0; 


    for (int i = 0; i < totalnumbers; i++)
        {
            flws[i] = new FlowLayoutPanel();
            flws[i].Name = "flw" + i;
            flws[i].Location = new Point(3,brh);
            flws[i].Size = new Size(317,122);
            flws[i].BackColor = Color.DarkCyan;
            flws[i].BorderStyle = BorderStyle.Fixed3D;
            flws[i].Disposed += Form1_Disposed;               
            flws[i].Click += new EventHandler(butns_Click);

            pics[i] = new PictureBox();
            pics[i].Location = new Point(953, 95 + brh);
            pics[i].Name = "pic" + i;
            pics[i].Size = new Size(300, 75);
            pics[i].ImageLocation = "E:/image"+i;
            flws[i].Controls.Add(pics[i]);

            txts[i] = new TextBox();
            txts[i].Name = "txt" + i;
            txts[i].Location = new Point(953, 186 + brh);
            txts[i].TextChanged += Form1_TextChanged;
            flws[i].Controls.Add(txts[i]);

            butns[i] = new Button();
            butns[i].Click += new EventHandler(butns_Click);
            butns[i].Text = "submit";
            butns[i].Name = "but" + i;
            butns[i].Location = new Point(1100, 186 + brh);

            flws[i].Controls.Add(butns[i]);
            this.Controls.Add(flws[i]);
            flowLayoutPanel1.Controls.Add(flws[i]);
            brh += 130;
        }  


 private void butns_Click(object sender, EventArgs e)
    {
        // you can add the procces to perform after dynamically created button pressed
     }
Share:
29,469
Codefun64
Author by

Codefun64

Updated on October 18, 2020

Comments

  • Codefun64
    Codefun64 over 3 years

    I'm trying to add pictureboxes dynamically to a C# win32 form for a near-production-quality application I'm trying to build, and I've got it pretty much down.

    The problem I'm running into is that I can't seem to add pictureboxes or controls to the form dynamically, in a method. I have added them to the form via the Form1 initializing method, but if I add controls in, say, a button_click method, it simply doesn't add them unless I have a panel container, and type panel1.Controls.Add(stuff). But, then the picturebox appears in a completely different location than intended - and blocked by, apparently, the panel itself. Most of the picturebox is blocked by the panel, and part of it is -outside- the panel. I have no idea what's going on.

    Here's the code I'm using that I learned online to add the pictureboxes, in a button click method:

    PictureBox pb = new PictureBox();
            pb.Size = new Size(this.Size.Width / 14, this.Size.Width / 12);  //I use this picturebox simply to debug and see if I can create a single picturebox, and that way I can tell if something goes wrong with my array of pictureboxes. Thus far however, neither are working.
            pb.BackgroundImage = Properties.Resources.cardback;
            pb.BackgroundImageLayout = ImageLayout.Stretch;
            pb.Location = new Point(50, 50);
            pb.Anchor = AnchorStyles.Left;
            pb.Visible = true;
            InitializeComponent();
            this.Controls.Add(pb);
            PictureBox[] pbName = new PictureBox[totaldeckcount];
            for (int i = 0; i < totaldeckcount; i++)
            {
                pbName[i] = new PictureBox();
                pbName[i].Size = new Size(this.Size.Width / 14, this.Size.Width / 12);
                pbName[i].BackgroundImage = Properties.Resources.cardback;
                pbName[i].BackgroundImageLayout = ImageLayout.Stretch;
                pbName[i].Image = Properties.Resources.cardback;
                pbName[i].Anchor = AnchorStyles.Left;
                pbName[i].Visible = true;
                int x = 0;
                int y = 15;
                if (i > 10)
                {
                    y += (int)((this.Size.Height * i) + 30);
                }
                x = (int)((this.Size.Width / 12) * Math.IEEERemainder(i, 10));
                pbName[i].Location = new Point(x, y);
                this.Controls.Add(pbName[i]);
            }
    

    Cardback is a working texture, I've seen the thing pop up when I try making a picturebox with it in Form1's method, so that's not the problem. The problem doesn't -appear- to be my syntax, since I was able to copy the

    PictureBox pb = new PictureBox();
    

    code directly into the Form1 method and it executed just fine.

    I can't find anything online via google, and this has me completely stumped.