Panel p = new Panel();
public Form4()
{
InitializeComponent();
p.Dock = DockStyle.Fill;
this.Controls.Add(p);
this.SizeChanged+=new EventHandler(Form4_SizeChanged);
}
private void Form4_SizeChanged(object sender, EventArgs e)
{
this.p.Controls.Clear();
int w = this.p.Width;
int h = this.p.Height;
int x = 0, y = 0;
int cs = 5, vs = 3;
int tb = 0;
for (; y < h; )
{
Application.DoEvents();
y += tb;
for (; x < w; )
{
Button b = new Button();
b.Padding = new Padding(cs, vs, 0, 0);
b.Location = new Point(x, y);
x += cs + b.Width;
if (vs + b.Height > tb)
tb = vs + b.Height;
if (x >= w) { x = 0; break; }
if (y + tb >= h) return;
this.p.Controls.Add(b);
Application.DoEvents();
}
}
}
Panel p = new Panel();
public Form4()
{
InitializeComponent();
p.Dock = DockStyle.Fill;
this.Controls.Add(p);
this.SizeChanged+=new EventHandler(Form4_SizeChanged);
}
private void b_Click(object sender, EventArgs e)
{
MessageBox.Show(((Button)sender).Text);
}
private void Form4_SizeChanged(object sender, EventArgs e)
{
this.p.Controls.Clear();
int w = this.p.Width;
int h = this.p.Height;
int x = 0, y = 0;
int cs = 5, vs = 3;
int tb = 0;
int n = 0;
for (; y < h; )
{
Application.DoEvents();
y += tb;
for (; x < w; )
{
Button b = new Button();
b.Width = new Random().Next(30,100);
b.Click += new EventHandler(b_Click);
b.Padding = new Padding(cs, vs, 0, 0);
b.Location = new Point(x, y);
x += cs + b.Width;
if (vs + b.Height > tb)
tb = vs + b.Height;
if (x >= w) { x = 0; break; }
if (y + tb >= h) return;
n++; b.Text = n.ToString();
this.p.Controls.Add(b);
Application.DoEvents();
}
}