matteo0402
matteo0402

Reputation: 7

Make dynamically created panels in winform responsive (same size as my flowlayoutPanel) - C#

So I'm creating some panels with the following code

private void button1_Click(object sender, EventArgs e)
{
int xlocation = 5;

        for (int i = 0; i < 10; i++)
        {
            Panel panel = new Panel();
            {
                panel.Name = string.Format("{0}", i);
                panel.Text = string.Format(i.ToString());
                panel.BackColor = Color.White;
                panel.Location = new System.Drawing.Point(xlocation, 30);
                panel.Width = flowLayoutPanel1.Width;
                panel.Height = 50;
                panel.MouseEnter += new System.EventHandler(this.panel_MouseEnter);
                flowLayoutPanel1.Controls.Add(panel);



                Label label = new Label();
                label.Location = new System.Drawing.Point(15, 10);
                label.AutoSize = true;
                label.Font = new System.Drawing.Font("Calibri", 13F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64);
                label.Text = string.Format("{0}", "GNU" + i);
                panel.Controls.Add(label);

                Label label10 = new Label();
                label10.Location = new System.Drawing.Point(15, 35);
                label10.AutoSize = true;
                label10.Font = new System.Drawing.Font("Calibri", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                label10.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64);
                label10.Text = string.Format("{0}", "hest");
                panel.Controls.Add(label10);
            }
            xlocation = xlocation + 85;
        }
   }

The problem is when I resize my form, my flowLayoutPanel obviously gets bigger, but the panels inside doesn't.

I have tried using the

private void Form1_Resize(object sender, EventArgs e)
    {
        
    }

But it doesn't seem to work.

Any answer is much appreciated! Thanks!

Upvotes: 0

Views: 611

Answers (2)

matteo0402
matteo0402

Reputation: 7

I found a solution. I just created a list with my panels like so

List<Panel> pnls = new List<Panel>();

And just did like so to make them follow the width of my flowlayoutpanel, so you can actually use a flowlayoutpanel ;-)

 private void Kontrol_Resize(object sender, EventArgs e)
    {
        for (int i = 0; i < pnls.Count; i++)
        {
            pnls[i].Width = activityData.Width - 24;
            pnls[i].Height = activityData.Height / 4;
        }
    }

Upvotes: 0

Usama Aziz
Usama Aziz

Reputation: 279

You can either use the Anchor property and set it to Right and Left, or can dock each panel to parent control. Something like this should work:

Panel panel = new Panel();
panel.Dock = DockStyle.Top; // Docks the panel to top, and fills to the width of Parent control
panel.Anchor = (AnchorStyles.Right | AnchorStyles.Left); // Set anchor to right and left

Upvotes: 1

Related Questions