我正在从事 ac# 练习项目,以发展我的技能并学习新事物。我创建了一个由几个控件组成的动态界面。当加载表单时,它以 numericUpDown 和 Button 启动。当用户在 numeric up down 上选择一个数字并单击按钮时,它将根据在 numericUpDown 上选择的数字生成尽可能多的文本框,它还会在生成的文本框旁边生成一个删除按钮。当用户单击删除按钮时,我无法删除文本框
这是我拥有的代码:
// 生成文本框和按钮
private void AssessmentButton_Click(object sender, EventArgs e)
{
int length = (int)this.NoAssesmentBoxlv4.Value;
for (int i = 0; i < length; i++)
{
textboxAssesmentName.Add(new TextBox());
var p = new System.Drawing.Point(110, 260 + i * 25);
(textboxAssesmentName[i] as TextBox).Location = p;
(textboxAssesmentName[i] as TextBox).Size = new System.Drawing.Size(183, 20);
this.Lv4Tab.Controls.Add(textboxAssesmentName[i] as TextBox);
buttoRemove.Add(new Button());
(buttoRemove[i] as Button).Location = new System.Drawing.Point(380, 260 + i * 25);
(buttoRemove[i] as Button).Text = @"x";
(buttoRemove[i] as Button).BackColor = Color.Red;
(buttoRemove[i] as Button).ForeColor = Color.White;
(buttoRemove[i] as Button).Size = new System.Drawing.Size(22, 23);
this.Lv4Tab.Controls.Add(buttoRemove[i] as Button);
(buttoRemove[i] as Button).Click += this.buttoRemove_click;
}
}
这是删除按钮单击的来源:(此方法无法编译)
private void buttoRemove_click(object sender, EventArgs e)
{
foreach (Object obj in textboxAssesmentName)
{
// THIS LINE DOES NOT COMPILE!!!
this.Controls.Remove(textboxAssesmentName.Remove);
}
}
任何想法将不胜感激