在FlowLayoutPanel
为这个做。
这是一个例子。我使用不同的方法BackColors
来展示每个新产品如何Panel
将先前的产品压低:
Random R = new Random(); private void button1_Click(object sender, EventArgs e) { Panel p = new Panel(); p.Name = "panel" + (flowLayoutPanel1.Controls.Count + 1); p.BackColor = Color.FromArgb(123, R.Next(222), R.Next(222)); p.Size = new Size(flowLayoutPanel1.ClientSize.Width, 50); flowLayoutPanel1.Controls.Add(p); flowLayoutPanel1.Controls.SetChildIndex(p, 0); // this moves the new one to the top! // this is just for fun: p.Paint += (ss, ee) => {ee.Graphics.DrawString(p.Name, Font, Brushes.White, 22, 11);}; flowLayoutPanel1.Invalidate(); }