One option would be to use the control Tablelayoutpanel with the property Dock to Fill
, has even the option to put the column width in percentage. Each cell only allows a control, use a container if you need more than one control per cell. Also put Dock to Fill
in the Controls...
Here is an example with Controls created in Runtime:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CreateControls();
}
private void CreateControls()
{
var tableLayoutPanel = new TableLayoutPanel
{
RowCount = 2,
ColumnCount = 3
};
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 45F));
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 10F));
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 45F));
tableLayoutPanel.RowCount = 2;
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F));
tableLayoutPanel.RowStyles.Add(new RowStyle());
var label = new Label { Text = "Grupo: LLL" };
var groupBox1 = new GroupBox { Dock = DockStyle.Fill };
var textBox1 = new TextBox { Dock = DockStyle.Top };
var dataGridView1 = new DataGridView { Dock = DockStyle.Fill };
// A ordem que os controlos são adicionados tem importancia
// o que tem 'DockStyle.Fill' tem de ser o primeiro a ser adicionado
groupBox1.Controls.Add(dataGridView1);
groupBox1.Controls.Add(textBox1);
var groupBox2 = new GroupBox { Dock = DockStyle.Fill };
var textBox2 = new TextBox { Dock = DockStyle.Top };
var dataGridView2 = new DataGridView { Dock = DockStyle.Fill };
groupBox2.Controls.Add(textBox2);
groupBox2.Controls.Add(dataGridView2);
// Em design mode, poderia ser 'Bring to Front'
groupBox2.Controls.SetChildIndex(dataGridView2, 0);
tableLayoutPanel.Controls.Add(label, 0, 0);
tableLayoutPanel.Controls.Add(groupBox1, 0, 1);
tableLayoutPanel.Controls.Add(groupBox2, 2, 1);
tableLayoutPanel.Dock = DockStyle.Fill;
tableLayoutPanel.Padding = new Padding(20);
this.Controls.Add(tableLayoutPanel);
}
}