C#表单应用程序动态添加元素定位
首先,我为梅拙劣的语言感到抱歉。我是C#新手,正在尝试构建一个简单的表单应用程序。单击按钮后,新的文本框和标签将显示在窗口中。问题就在这里。当我向右滑动并单击按钮时,新添加的元素位于其他元素的右侧。你能帮我解决这个问题吗C#表单应用程序动态添加元素定位,c#,winforms,C#,Winforms,首先,我为梅拙劣的语言感到抱歉。我是C#新手,正在尝试构建一个简单的表单应用程序。单击按钮后,新的文本框和标签将显示在窗口中。问题就在这里。当我向右滑动并单击按钮时,新添加的元素位于其他元素的右侧。你能帮我解决这个问题吗 您没有布局容器,例如TableLayoutPanel。 这些布局容器将帮助您定位动态添加的控件。 我正在粘贴我为动态添加控件所做的一些示例代码: internal void AddControl(Models.CdConfig selectedCd) {
您没有布局容器,例如TableLayoutPanel。 这些布局容器将帮助您定位动态添加的控件。 我正在粘贴我为动态添加控件所做的一些示例代码:
internal void AddControl(Models.CdConfig selectedCd)
{
SelectedCds.Add(selectedCd);
if (selectedCd.DataType == CdType.Combo || selectedCd.DataType == CdType.Choice)
{
subItemHeight = 23;
}
else
{
subItemHeight = 30;
}
int currItemRowCount = getItemRowCount(selectedCd);
Panel controlPanel = new Panel() // the panel that is visible
{
BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle,
Location = new Point(3,3),
Size = new Size(this.Size.Width - margin * 2 * 2 - scrollbarbuff, itemHeight + subItemHeight * currItemRowCount)
};
TableLayoutPanel t1 = new TableLayoutPanel() // main tlp
{
ColumnCount = 1,
RowCount = 2,
Size = new Size(this.Size.Width - margin * 2 * 2 - scrollbarbuff, itemHeight + subItemHeight * currItemRowCount)
};
t1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
t1.RowStyles.Add(new RowStyle(SizeType.Absolute, itemHeight));
t1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
TableLayoutPanel tHeader = new TableLayoutPanel() // label and delete button
{
ColumnCount = 2,
RowCount = 1,
Size = new Size(this.Size.Width - margin * 2 * 2 - scrollbarbuff, itemHeight)
};
tHeader.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 80F));
tHeader.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20F));
tHeader.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
t1.Controls.Add(tHeader, 0, 0);
// Add the label
Label lbl = new Label()
{
Text = selectedCd.DisplayName,
Anchor = AnchorStyles.Left | AnchorStyles.Top,
Margin = new Padding(0, 3 * 2, 0, 0)
};
tHeader.Controls.Add(lbl, 0, 0);
Button deleteBtn = new Button()
{
Text = "Delete",
Anchor = AnchorStyles.Right | AnchorStyles.Top,
Margin = new Padding(0, 2, 3 * 2, 0)
};
deleteBtn.Tag = controlPanel;
deleteBtn.Click += HandleDelete;
tHeader.Controls.Add(deleteBtn, 1, 0);
controlPanel.Controls.Add(t1);
// For the control
TableLayoutPanel tControl = CreateCdControl(selectedCd, currItemRowCount);
t1.Controls.Add(tControl, 0, 1);
this.Controls.Add(controlPanel);
controlPanel.Tag = selectedCd; // for convenience
}
添加控件时,请考虑属性的值
/// <summary>
/// Make a point that gives a location at the given coordinates in the form
/// independent of the current scroll position
/// </summary>
private Point MakeAbsolutePosition(int x, int y)
{
Point result = new Point(x + AutoScrollPosition.X, y + AutoScrollPosition.Y);
return result;
}
private void btnEkle_Click(object sender, EventArgs e)
{
TextBox isimTb = new TextBox();
TextBox fiyatTb = new TextBox();
Label urunLbl = new Label();
int positionNo = formNo;
isimTb.Name = "isimBox" + formNo.ToString();
isimTb.Location = MakeAbsolutePosition(125, ((positionNo - 1) * 25));
isimTb.Width = 200;
isimTb.Text = "Ürün İsmini Giriniz";
fiyatTb.Name = "fiyatBox" + formNo.ToString();
fiyatTb.Location = MakeAbsolutePosition(350, (positionNo - 1) * 25);
fiyatTb.Width = 200;
fiyatTb.Text = "Ürün Fiyatını Giriniz";
urunLbl.Name = "label" + formNo.ToString();
urunLbl.Text = formNo.ToString() + ". Ürün";
urunLbl.Location = MakeAbsolutePosition(10, (positionNo - 1) * 25);
urunLbl.Width = 100;
this.Controls.Add(urunLbl);
this.Controls.Add(isimTb);
this.Controls.Add(fiyatTb);
// TODO: Use the correct x values here
btnEkle.Location = MakeAbsolutePosition(100, (positionNo - 1) * 25 + 50);
btnKaydet.Location =MakeAbsolutePosition(200, (positionNo - 1) * 25 + 50);
formNo++;
}
//
///在表格中的给定坐标处创建一个位置点
///与当前滚动位置无关
///
专用点绝对位置(整数x,整数y)
{
点结果=新点(x+AutoScrollPosition.x,y+AutoScrollPosition.y);
返回结果;
}
私有void btnEkle_单击(对象发送方,事件参数e)
{
TextBox isimTb=新的TextBox();
TextBox fiyatTb=新的TextBox();
Label urunLbl=新标签();
int positionNo=formNo;
isimTb.Name=“isimBox”+formNo.ToString();
isimTb.Location=MakeAbsolutePosition(125,((位置编号-1)*25));
isimTb.宽度=200;
isimTb.Text=“Ürünİsmini girizing”;
fiyatTb.Name=“fiyatBox”+formNo.ToString();
fiyatTb.位置=绝对位置(350,(位置编号-1)*25);
fiyatTb.宽度=200;
Text=“Ürün Fiyatınıgirizing”;
urunLbl.Name=“label”+formNo.ToString();
urunLbl.Text=formNo.ToString()+“.Ürün”;
urunLbl.位置=绝对位置(10,(位置编号-1)*25);
urunLbl.宽度=100;
this.Controls.Add(urunLbl);
this.Controls.Add(isimTb);
this.Controls.Add(fiyatTb);
//TODO:在此处使用正确的x值
btnEkle.Location=MakeAbsolutePosition(100,(位置编号-1)*25+50);
btnKaydet.位置=绝对位置(200,(位置编号-1)*25+50);
formNo++;
}
委员会:
以编程方式向窗体添加控件时,请使用AutoScrollPosition属性将控件定位在当前可查看滚动区域的内部或外部
如其他答案所示,使用布局面板(如或)的方法更好。如果可能的话,您应该使用它。代码中似乎没有任何东西会导致图像中显示的内容。请您创建一个新项目,并通过此问题中的代码,看看是否得到相同的结果?@Enigmativity将表单的AutoScroll设置为true,然后可以重现此问题。非常感谢您的回答。这个解决方案对我来说是最简单有效的解决方案。谢谢你的回答。我将学习布局容器,并使用您的代码作为参考。
/// <summary>
/// Make a point that gives a location at the given coordinates in the form
/// independent of the current scroll position
/// </summary>
private Point MakeAbsolutePosition(int x, int y)
{
Point result = new Point(x + AutoScrollPosition.X, y + AutoScrollPosition.Y);
return result;
}
private void btnEkle_Click(object sender, EventArgs e)
{
TextBox isimTb = new TextBox();
TextBox fiyatTb = new TextBox();
Label urunLbl = new Label();
int positionNo = formNo;
isimTb.Name = "isimBox" + formNo.ToString();
isimTb.Location = MakeAbsolutePosition(125, ((positionNo - 1) * 25));
isimTb.Width = 200;
isimTb.Text = "Ürün İsmini Giriniz";
fiyatTb.Name = "fiyatBox" + formNo.ToString();
fiyatTb.Location = MakeAbsolutePosition(350, (positionNo - 1) * 25);
fiyatTb.Width = 200;
fiyatTb.Text = "Ürün Fiyatını Giriniz";
urunLbl.Name = "label" + formNo.ToString();
urunLbl.Text = formNo.ToString() + ". Ürün";
urunLbl.Location = MakeAbsolutePosition(10, (positionNo - 1) * 25);
urunLbl.Width = 100;
this.Controls.Add(urunLbl);
this.Controls.Add(isimTb);
this.Controls.Add(fiyatTb);
// TODO: Use the correct x values here
btnEkle.Location = MakeAbsolutePosition(100, (positionNo - 1) * 25 + 50);
btnKaydet.Location =MakeAbsolutePosition(200, (positionNo - 1) * 25 + 50);
formNo++;
}