Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to generating 96 FlowLayoutPanel inside another FlowLayoutPanel, right now I have the size of each FlowLayoutPanel is given fl.Size = new Size(1000, 30);. I need help to change size fl.Size = new Size(Autofill, 30);. so it can fill up the width instead of given a static width size.

Thankyou

What I have tried:

private List<FlowLayoutPanel> listFl96 = new List<FlowLayoutPanel>();
private void Gennerate96FL(int total96)
        {
            fl96.Controls.Clear();
            listFl96.Clear();
            for (int i = 1, loopTo = total96; i <= loopTo; i++)
            {
                var fl = new FlowLayoutPanel();
                fl.Name = $"fl96{i}";
                fl.Size = new Size(1000, 30);
                fl.BackColor = Color.Cornsilk;
                fl.BorderStyle = BorderStyle.FixedSingle;
                fl.Cursor = Cursors.Hand;
                fl.AutoScroll = true;
                fl96.Controls.Add(fl);
                listFl96.Add(fl);
            }
        }
Posted
Updated 11-Mar-20 2:55am

1 solution

Hi,

As far as I know there is no good way to express this in Windows.Forms with FlowLayoutPanel because normal "DockStyle.Top" won't work.

so here is a possible example solution:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();


        master.Dock = DockStyle.Fill;
        master.SizeChanged += Master_SizeChanged;
        this.Controls.Add(master);

        m_iWidth = master.Width;

        Gennerate96FL(96);
    }

    private void Master_SizeChanged(object sender, EventArgs e)
    {
        if(m_iWidth != master.Width)
        {
            m_iWidth = master.Width;
            foreach (FlowLayoutPanel flp in listFl96)
                flp.Width = m_iWidth;
        }
    }

    int m_iWidth;
    FlowLayoutPanel master = new FlowLayoutPanel();
    private List<FlowLayoutPanel> listFl96 = new List<FlowLayoutPanel>();

    private void Gennerate96FL(int total96)
    {
        master.Controls.Clear();
        listFl96.Clear();
        for (int i = 1, loopTo = total96; i <= loopTo; i++)
        {
            var fl = new FlowLayoutPanel();
            fl.Name = $"fl96{i}";
            fl.Height = 30;
            fl.BackColor = Color.Cornsilk;
            fl.BorderStyle = BorderStyle.FixedSingle;
            fl.Cursor = Cursors.Hand;
            fl.AutoScroll = true;
            fl.Width = m_iWidth;

            master.Controls.Add(fl);
            listFl96.Add(fl);
        }
    }
}


So we set the initial Width of the "child" FlowLayoutPanels to the Width of it's parent FlowLayoutPanel (I called it "master"), and to react to any Width-Changes for the "master" FlowLayoutPanel (which then updates the "childs" Width-properties)
Not a nice solution but it works...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900