使在winform中动态创建的面板响应(与我的flowlayoutPanel大小相同)-C#

使在winform中动态创建的面板响应(与我的flowlayoutPanel大小相同)-C#,c#,winforms,responsive,dynamically-generated,flowlayoutpanel,C#,Winforms,Responsive,Dynamically Generated,Flowlayoutpanel,所以我用下面的代码创建了一些面板 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}&

所以我用下面的代码创建了一些面板

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;
        }
   }
但它似乎不起作用

非常感谢您的回答!
谢谢

您可以使用Anchor属性并将其设置为Right和Left,也可以将每个面板停靠到父控件。像这样的方法应该会奏效:

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

我找到了解决办法。我刚刚用我的面板创建了一个列表,如下所示

List<Panel> pnls = new List<Panel>();
List pnls=new List();
这样做只是为了让它们跟随我的flowlayoutpanel的宽度,这样你就可以实际使用flowlayoutpanel;-)

private void Kontrol\u Resize(对象发送方,事件参数e)
{
对于(int i=0;i
@JohnWu不起作用,如果我删除for循环,它只在最新创建的面板上起作用。当我将表单变大或变小时,我希望所有面板的大小调整为与flowlayoutpanel相同的宽度。flowlayoutpanel的属性有限。我发现,如果我使用常规面板制作自己的面板阵列,我有更多的属性,通常可以解决这些问题。每次我尝试使用flowLayoutPanel时,我都会发现我无法完成我想要的任务,并最终更改为常规面板。请再次阅读我对您的评论:1)创建用户控件,2)不要为此使用flowLayoutPanel。使用标准面板或TableLayoutPanel。
List<Panel> pnls = new List<Panel>();
 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;
        }
    }