我正在尝试在用户单击按钮时创建一个新的文本框行。但是,如果用户向下滚动然后单击按钮,则新文本框似乎低于我希望它们。我如何处理这个以便文本框对齐?
private void button1_Click(object sender, EventArgs e)
{
TotalNumberAdded++;
AddControls(TotalNumberAdded);
}
private void AddControls(int rowCounter)
{
// Convert 'rowCounter' to string
string counter = rowCounter.ToString();
// Create new TextBox
var newTextbox = new TextBox();
//panel1.Controls.Add(newTextbox);
newTextbox.Name = "txt_" + counter;
//newTextbox.Location = new Point(60, y);
newTextbox.Anchor = (AnchorStyles.Top | AnchorStyles.Left);
newTextbox.Top = y;
newTextbox.Left = 60;
// Add items to panel, then add panel to form
this.panel1.Controls.Add(newLabel);
this.panel1.Controls.Add(newTextbox);
y = y + 40;
}
答案 0 :(得分:0)
我想出来了。在面板中创建锚标签,并使用当前相对位置newTextbox.Top = mAnchor.Location.Y + y;
作为Y轴。