我有一堆动态添加的控件,当用户单击添加用户按钮时会逐行添加。我希望在页面加载时有标签,并且我希望每次单击添加用户按钮时(在每行文本框下)都会向下移动相同的标签。现在它正在加载,当用户第一次点击按钮时它会向下移动但在此之后它就会停留。这是我的代码:
全局变量:
Label Savelbl = new Label();
int LabelX = 15;
int LabelY = 110;
int spacelbl = 15;
页面加载:
Savelbl.Location = new Point(LabelX, LabelY);
Savelbl.Name = "Savelbl";
Savelbl.Text = "Please click 'save' to save your changes";
CaeUsersPanel.Controls.Add(Savelbl);
添加用户按钮:
private void CAEAddUserbtn_Click(object sender, EventArgs e)
{
var i = UsernameTextBoxes.Count + 1; // this is a list of the added textboxes
ADDUserInfo(i); //method which adds the dynamically created textboxes
Savelbl.Location = new Point(LabelX, LabelY + spacelbl);
}
删除用户按钮(单击此标签时标签应向上移动):
private void Remove_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure you want delete this user? \n Deleting users may break workflows", "Delete", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
int idx = RemoveButtons.IndexOf((Button)sender);
// Remove button
RemoveButtons[idx].Dispose();
RemoveButtons.RemoveAt(idx);
// Remove textbox
UsernameTextBoxes[idx + 1].Dispose();
UsernameTextBoxes.RemoveAt(idx + 1);
//Shift controls up
for (int i = idx; i < RemoveButtons.Count; i++)
{
UsernameTextBoxes[i + 1].Top -= SpaceDelta;
}
space -= SpaceDelta;
Savelbl.Location = new Point(LabelX, LabelY - spacelbl);
}
}
答案 0 :(得分:1)
您永远不会更新LabelX和LabelY。
LabelX = Savelbl.Location.X
LabelY = Savelbl.Location.Y
Savelbl.Location = new Point(LabelX, LabelY - spacelbl);
你也可以摆脱这些变量......