以下代码创建动态代码,其中控件的数量与数据库不同。
FlowLayoutPanel flowlayout = new FlowLayoutPanel();
flowlayout.Dock = DockStyle.Fill;
flowlayout.AutoScroll = true;
while (reader.Read())
{
Button button = new Button();
button.Size = new Size(500, 200);
button.Name = reader["fname"].ToString();
button.Text = reader["userID"].ToString();
button.BackColor = Color.Black;
button.ForeColor = Color.White;
button.Dock = DockStyle.Top;
flowlayout.Controls.Add(button);
}
panel1.Controls.Add(flowlayout);
然后我想将此控件添加到在运行时创建的动态Panel控件中。