0

Let's say I have X number of buttons to add to my Form programmatically;

What I would like is that all the controls have the same size and that they fill the form completely depending on the form size, for example with 4 buttons :

enter image description here

9 buttons :

enter image description here

Stefano d'Antonio
  • 5,874
  • 3
  • 32
  • 45
Keytrap
  • 320
  • 3
  • 11

1 Answers1

4

To layout controls you can use a TableLayoutPanel

var tableLayoutPanel = new TableLayoutPanel
{
    Dock = DockStyle.Fill,
    RowCount = 2,
    ColumnCount = 2
};

tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50));
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));

tableLayoutPanel.Controls.Add(new Button { Dock = DockStyle.Fill });
tableLayoutPanel.Controls.Add(new Button { Dock = DockStyle.Fill });
tableLayoutPanel.Controls.Add(new Button { Dock = DockStyle.Fill });
tableLayoutPanel.Controls.Add(new Button { Dock = DockStyle.Fill });

yourForm.Controls.Add(tableLayoutPanel);

It will also keep the aspect if you resize the form.

Stefano d'Antonio
  • 5,874
  • 3
  • 32
  • 45