我正在尝试自动生成并在面板上很好地显示这些控件(panelMetaData)。 一切都出现在屏幕上并且有些对齐,而一些标签文本覆盖了其他文本等。我该怎么做才能使这个控件正确定位?我缺少任何位置逻辑吗?
panelMetaData 是使用(拖放)工具箱添加的,具有 、Dock:Fill、AutoSize:True 和 等属性自动滚动:真。
这是创建控件并将它们放置在面板中的方法:(我删除了一些代码以使阅读尽可能简单)
private void createAttributePanelControls()
{
txtboxes = new List<Control>();
labels = new List<Control>();
valLabel = new List<Control>();
combobox = new List<Control>();
datetimetxtboxes = new List<Control>();
int stringcnt = 0;
int datecnt = 0;
int listcnt = 0;
for (int i = 0; i < attributes.Length; i++)
{
Attribute attribute = attributes[i];
labels.Add(CreateLabel(attribute.getDisplayName(), i, new Point(26, 56 + (i * 30))));
panelMetaData.Controls.AddRange(labels.ToArray());
switch (attribute.getType())
{
case DocAttribute.STRING:
txtboxes.Add(CreateTextBox(attribute.getDisplayName(), i, new Point(labels[i].Width, labels[i].Top - 3)));
valLabel.Add(CreateValidationLabel(attribute.getDisplayName(), i, new Point(txtboxes[stringcnt].Width, labels[i].Top - 3)));
stringcnt++;
break;
case DocAttribute.DATE:
datetimetxtboxes.Add(CreateDateTimePicker(attribute.getDisplayName(), i, new Point(labels[i].Widt, 56 + (i * 30))));
valLabel.Add(CreateValidationLabel(attribute.getDisplayName(), i, new Point(datetimetxtboxes[datecnt].Width, labels[i].Top - 3)));
datecnt++;
break;
case DocAttribute.LIST:
combobox.Add(CreateComboBox(attribute.getDisplayName(), i, new Point(labels[i].Widt, 56 + (i * 30))));
valLabel.Add(CreateValidationLabel(attribute.getDisplayName(), i, new Point(combobox[listcnt].Width, labels[i].Top - 3)));
listcnt++;
break;
}
}
if (txtboxes.Any()) panelMetaData.Controls.AddRange(txtboxes.ToArray());
if (datetimetxtboxes.Any()) panelMetaData.Controls.AddRange(datetimetxtboxes.ToArray());
if (combobox.Any()) panelMetaData.Controls.AddRange(combobox.ToArray());
if (valLabel.Any()) panelMetaData.Controls.AddRange(valLabel.ToArray());
}
这些是创建方法:
private TextBox CreateTextBox(string text, int value, Point location)
{
TextBox textbox = new TextBox();
textbox.Name =//somelogic
textbox.Location = new Point(location.X, location.Y);
textbox.Size = new Size(300, 21);
return textbox;
}
private DateTimePicker CreateDateTimePicker(string text, int value, Point location)
{
DateTimePicker datetextbox = new DateTimePicker();
datetextbox.Name = //somelogic
datetextbox.Location = new Point(location.X, location.Y);
datetextbox.Size = new Size(300, 21);
return datetextbox;
}
private ComboBox CreateComboBox(string text, int value, Point location)
{
ComboBox combobox = new ComboBox();
combobox.Name = //somelogic;
combobox.Location = new Point(location.X, location.Y);
combobox.Size = new Size(300, 21);
return combobox;
}
private Label CreateLabel(string text, int value, Point location)
{
Label label = new Label();
label.Name =//somelogic;
label.Location = new Point(location.X, location.Y);
label.AutoSize = true;
return label;
}
private Label CreateValidationLabel(string text, int value, Point location)
{
Label label = new Label();
label.Name = "vlb" + (text.Contains(".") ? text.Substring(0, text.IndexOf(".")) : text);
label.Text = "*";
label.Tag = value;
label.Location = new Point(location.X, location.Y);
label.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label.ForeColor = System.Drawing.Color.Red;
label.AutoSize = true;
return label;
}
请您参考如下方法:
在阅读答案之前,最好考虑一下:
- 您可以使用
TableLayoutPanel更简单地布置您的控件有 2 列。 - 还可以使用
ErrorProvider来显示验证消息并使用错误提供程序设置错误或清除控件的错误SetError方法。
但是我在您的代码中看到的错误逻辑是:
当您将 AutoSize 属性设置为 true 时,您还没有为标签设置任何文本,然后将它们添加到 panelMetaData.Controls 并且它使它们的大小为 (0,0)。然后您使用标签的 Width 来定位控件,并且由于标签的 Width 为 0,您的控件将显示为 Location .X = 0
