我试图在从左到右的流面板中“愉快地”对齐控件的顶部。
我有一个复选框和几个numericUpDowns。但是复选框的顶部总是有某种边距或填充。因此,我必须在numericUpDown的顶部添加margn,以使它们排成一列。
这并不理想。为什么我不能像numericUpDown那样让checkBox直接从flowpanel的顶部开始呢?
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);
}
}

发布于 2018-02-05 23:47:23
这不是顶部应该对齐。文本的基线是各种控件之间应该对齐的内容。如果您使用较大的字体,这一点会更加明显。

https://stackoverflow.com/questions/48621838
复制相似问题