我试图在一个从左到右的流动板中“愉快地”对齐控制顶部。
我有一个复选框和几个numericUpDowns。但是复选框始终在顶部有某种边距或填充。因此,我必须在numericUpDown的顶部添加margn以使它们排成一行。
这不太理想。为什么我不能让checkBox直接从flowpanel顶部开始,就像numericUpDown一样?
public partial class Form1 : Form
{
public static FlowLayoutPanel ControlPanel;
public void CreateControls()
{
ControlPanel = new FlowLayoutPanel();
ControlPanel.SuspendLayout();
ControlPanel.FlowDirection = FlowDirection.LeftToRight;
ControlPanel.Height = 40;
ControlPanel.Width = this.flowLayoutPanel1.Width - 10;
ControlPanel.WrapContents = false;
ControlPanel.AutoScroll = true;
ControlPanel.Anchor = AnchorStyles.None;
ControlPanel.BorderStyle = BorderStyle.FixedSingle;
ControlPanel.Controls.Add(LabelWithText("LB", 60));
AddNumericUpDown("Ledge", 20, 0, 1m, -500m, 500m);
AddCheckBox("UseExit", "UseExit", false);
AddNumericUpDown("WrongLedge", 30, 0, 1m, -500m, 500m);
AddNumericUpDown("Max", 100000, 0, 100000m, 0m, 1000000m);
ControlPanel.ResumeLayout();
this.flowLayoutPanel1.Controls.Add(ControlPanel);
}
public Label LabelWithText(string text, int width)
{
Label label = new Label();
label.Text = text;
label.Width = width;
label.Top = 0;
return label;
}
public void AddCheckBox(string name, string text, bool initialValue)
{
CheckBox checkBox = new CheckBox();
checkBox.Name = name;
checkBox.Text = text;
checkBox.Checked = initialValue;
checkBox.Top = 0;
checkBox.Margin = new Padding(5,0,0,0);
checkBox.TextAlign = ContentAlignment.TopLeft;
checkBox.CheckAlign = ContentAlignment.TopLeft;
checkBox.ImageAlign = ContentAlignment.TopLeft;
ControlPanel.Controls.Add(checkBox);
}
public void AddNumericUpDown(string name, double initialValue, int decimalPlaces, decimal increment, decimal minimum, decimal maximum)
{
NumericUpDown numericUpDown = new NumericUpDown();
numericUpDown.DecimalPlaces = decimalPlaces;
numericUpDown.Increment = increment;
numericUpDown.Minimum = minimum;
numericUpDown.Maximum = maximum;
numericUpDown.Value = (decimal)initialValue;
numericUpDown.Name = name;
numericUpDown.Visible = true;
numericUpDown.Margin = new Padding(2);
numericUpDown.Top = 0;
numericUpDown.Width = 80;
ControlPanel.Controls.Add(LabelWithText(name, 70));
ControlPanel.Controls.Add(numericUpDown);
}
}