在运行时创建控件

Viv*_*iva 1 .net c# foreach textbox

我无法设法从textboxes运行时创建的值.

我希望用户从a中选择一些内容checkedlistbox,并textboxes在每次单击按钮时输入他想要的任何值.

我怎么能得到那些名字texboxes?他们真的存在吗?我是初学者,我真的不明白它们是如何创建的.

这是我创建的代码textboxes.

    private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int x = 466;
        int y = 84;
        foreach (var itemChecked in checkedListBox1.CheckedItems)
        {
            int i = 0;
            TextBox tb = new TextBox();
            tb.Location = new Point(x, y);
            tb.Name = "txtBox" + i++.ToString();
            Controls.Add(tb);
            y += 30;
     }
Run Code Online (Sandbox Code Playgroud)

spa*_*jce 5

只需将i外部放置foreach并完成.

int i = 0;
foreach (var itemChecked in checkedListBox1.CheckedItems)
{
    i++;
    string textBoxName = "textBox" + i.ToString();
    TextBox tb = new TextBox();
    tb.Location = new Point(x, y);
    //tb.Name = "txtBox" + i++.ToString(); <--Your Version
    tb.Name = textBoxName;
    //...
    //Other stuff or your codes
}
Run Code Online (Sandbox Code Playgroud)