简体   繁体   中英

Margins on Flow Layout Panel not Producing Expected Behavior

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.

Flowlayout面板边距

// 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM