动态命名变量内部for循环

时间:2012-09-15 17:08:01

标签: c# windows-phone-7

我想在for循环中创建16个Textbox,名为textbox1textbox16,运行16次。因此,在每个循环中创建1 textbox。怎么做到这一点?

2 个答案:

答案 0 :(得分:2)

您无法制作动态命名的变量。在这种情况下,将控件保留在某个集合中是最有意义的,例如在List<T>中:

List<TextBox> textBoxes = new List<TextBox>();
for(int i = 1 ; i <= 16 ; i++ )
{
    var tb = new TextBox() { Name = "textbox" + i };
    textBoxes.Add(tb);
}

答案 1 :(得分:0)

试试这个:

for(int counter=0;counter<16;counter++){
    TextBox TB = new TextBox();
    TB.Id = "textbox" + (counter + 1);

    // code to add this textbox in screen

}
相关问题