Reputation: 303
I am new to C# and I am using windows forms.
As shown in screenshot
I have Form1
which has flowLayoutPanel1
and ButtonCancel
.
When Form1
loads, number of buttons
are added into the flowLayoutPanel1
(the number of buttons is changing and it is not fixed).
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i <= 1; i++)
{
Button btn = new Button();
btn.Name = i.ToString();
btn.Width = 104;
btn.Height = 63;
btn.FlatStyle = FlatStyle.Popup;
flowLayoutPanel1.Controls.Add(btn);
}
}
Problem:
The problem is that there is a gap that I want to remove, to do that I have to reduce Form1
Height according to the number of the added buttons
but I do not know how to resize Form1
Height based on the number of added buttons
.
For example, if 2 buttons
are added I want Form1
to shrink to fit the 2 buttons
and if 8 buttons
are added I want Form1
to expand to fit the 8 buttons
without leaving gap.
Is there any Form
property
allows Form1
to expand and shrink according to the number of added buttons
?
Thank you
Upvotes: 1
Views: 116
Reputation: 7537
The following should work
TableLayoutPanel
with one column and two rows on the FormDock
-property of the TableLayoutPanel
to Fill
Size-Type
of the first row of the TableLayoutPanel
to Percent -> 100%Size-Type
of the second row of the TableLayoutPanel
to Absolute -> 63 PixelFlowLayoutPanel
inside the first rowButton
inside the second rowAutoSize
-property of Form
, TableLayoutPanel
and FlowLayoutPanel
to True
AutoSizeMode
-property of Form
, TableLayoutPanel
and FlowLayoutPanel
to GrowAndShrink
Upvotes: 1