Suryakavitha
Suryakavitha

Reputation: 1411

Problem in generating label controls in Windows formsApplication

I have created pictureboxes dynamically in windows forms Application... its working well... now i have to generate label boxes dynamically and that label boxes will display in bottom of the each picture box and that have to display the corressponding picturebox name... how shall i do this? Give me the solution of this...

Thanks In Advance... And My coding Is here..

           int Left = this.Left+200;
           int Top = this.Top;

           for(int i=0;i<Num_Picbox;i++)
           {
           shapes[i] = new PictureBox();
           shapes[i].Location = new Point(Left,Top);
           Left += 200;
           Top += i + 0;

           shapes[i].Size = new Size(160,160);
           shapes[i].BackColor = Color.Black;
           shapes[i].Visible = true;
           shapes[i].Name = i.ToString();
           shapes[i].BorderStyle = BorderStyle.FixedSingle;

           flowLayoutPanel1.Controls.Add(shapes[i]);
           flowLayoutPanel1.WrapContents = true;
           shapes[i].Click += new EventHandler(PictureBox_Click);
          }

Upvotes: 0

Views: 164

Answers (2)

Nathan Baulch
Nathan Baulch

Reputation: 20683

Create a panel for each picture that contains a label docked to Bottom and your picture box docked to Fill. Add this panel to the flow layout panel instead of your picture box. Make sure you add your picture box to the panel's Controls collection first to ensure that the docking works as expected.

shapes[i].Dock = DockStyle.Fill;
flowLayoutPanel1.Controls.Add(
    new Panel {
        Controls = {
            shapes[i],
            new Label {
                Dock = DockStyle.Bottom,
                Text = i.ToString()
            }
        }
    });

Upvotes: 1

Patrick Kafka
Patrick Kafka

Reputation: 9892

I think the best way would be to create a custom user control with picturebox and a label laid out the way you want then add them dynamically to the flowlayoutpanel. making shapes[] an array of the custom control type.

Upvotes: 2

Related Questions