【问题标题】:Deleting dynamically created textbox删除动态创建的文本框
【发布时间】:2020-01-03 16:59:48
【问题描述】:

大家好。

我正在创建函数,在该函数中我根据下拉列表中的选定值动态生成文本框。

这是代码。

comboboxNameHolder = ((ComboBox)sender).Name;
string comboboxNoHolder =comboboxNameHolder.Replace("cbFunctionList", "");
comboboxNo = Int32.Parse(comboboxNoHolder);
funcSelected = ((ComboBox)sender).SelectedItem.ToString();
for (int i = 0; i < optionList1.GetLength(0); i++)
{
    if (funcSelected == optionList1[i, 0])
    {
        funcNoOfFields = optionList1[i, 1];
    }
}

if (lineFieldController[comboboxNo, 1] == 0)
{
    fieldCounter = Int32.Parse(funcNoOfFields);
    lineFieldController[comboboxNo, 1] = fieldCounter;
    inputField1 = new TextBox[fieldCounter];
    for (int i = 0; i < fieldCounter; i++)
    {
        btnAddField0.Visible = false;
        inputField = new TextBox();
        inputField.Font = new Font("Microsoft Sans Serif", 11.25f);
        inputField.Size = new Size(75, 24);
        inputField.Location = new Point(positionController[comboboxNo, 0], positionController[comboboxNo, 1]);
        inputField.Name = "txtLine" + comboboxNo.ToString() + "Variable" + i.ToString();


        this.Controls.Add(inputField1[i]);
        positionController[comboboxNo, 0] += 81;
    }
}

现在我想在lineFieldController 不等于零时使用相同的函数,这意味着该行中已经创建了文本框。当用户在下拉列表中选择另一个值时,字段数将通过删除现有字段然后根据所选项目创建新字段来更改。

如何删除我创建的文本框?我试过用名字来称呼它,但它不起作用。

else
{
    for(int i = 0; i < lineFieldController[comboboxNo, 1]; i++)
    {
        string name = "txtLine" + comboboxNo.ToString() + "Variable" + i.ToString();
        TextBox tb = this.Controls.Find(name, true);
    }
}

希望得到您的回复

【问题讨论】:

  • 但它不起作用 ...抛出异常?返回空?有什么问题?我看不到任何删除/处置控件的代码行。您对控件名称的期望是否正确?您是否检查过您在 Controls 集合中拥有的控件的名称是什么?你调试过代码吗?

标签: c# .net winforms


【解决方案1】:

您可以将您创建的所有控件放入列表中,并保留对在运行时创建的控件的引用。

喜欢

public class Form1
{
List<Control> createdList = new List<Control>(); // class field

void combobox_SelectedIndexChanged()
{
            // removing controls were created before
            foreach (var created in createdList)
            {
              this.Controls.Remove(created);
              created.Dispose();
            }
            createdList.Clear(); // all created controls from previous index changed should be removed here

            // add each control you are creating to the createList additionally
            inputField1 = new TextBox[fieldCounter];
            for (int i = 0; i < fieldCounter; i++)
            {
                btnAddField0.Visible = false;
                inputField = new TextBox();
                createdList.Add(inputField); //store reference

/// skipping init code

                this.Controls.Add(inputField1[i]);
                positionController[comboboxNo, 0] += 81;
            }

}

}

另一个选项是在表单上添加面板作为所有正在创建的控件的占位符。您必须将 this.Controls.Add(inputField1[i]); 更改为 panelCreated.Controls.Add(inputField1[i]); 然后,您可以从面板中获取所有控件并删除它们,而无需像下面那样进行名称搜索

foreach (Control created in panelCreated.Controls)
  created.Dispose();
panelCreated.Controls.Clear();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    相关资源
    最近更新 更多