1
votes

Context: I'm using a flowlayoutpanel to dynamically add controls to a form.

Question: Why does setting margins on each control as I'm doing using the code below, not change how my controls appear in the attached image?

Thoughts: Using the code below should force the label and textbox to be right next to each other. It is my understanding that the margins are what affect the distance between each item for layout in a flowlayoutpanel.

Flowlayoutpanel margins

// Create the control instances. 
var textBox = new TextBox();
var nameLabel = new Label();

// Setup options for controls.
textBox.Size = new System.Drawing.Size(175, 20);
textBox.Margin = new Padding(0, 0, 0, 0);
nameLabel.Text = parameter.ParameterName;
nameLabel.Margin = new Padding(0, 0, 0, 0);

// Add controls to the flow panel. 
flowLayoutPanel1.Controls.Add(nameLabel);
flowLayoutPanel1.Controls.Add(textBox);

Rerferences:

Align dynamically added controls horizontally and vertically within a control in c# winforms

Adjusting spacing between usercontrols in a flowLayoutPanel

setting more space between controls in a flowLayout

1

1 Answers

2
votes

Your Label doesn't have a size, so its Height property is bigger than you think. Try changing the backcolor property of the label to see how much space it is occupying.

You can either set the size:

nameLabel.Size = new Size(175, 16);

or change the alignment:

nameLabel.TextAlign = System.Drawing.ContentAlignment.BottomLeft;

or both.