public void test()
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
for (int i = 1; i <= list.Count; i++)
{
textBx.Text = list[i].ToString();
// I want it to be textBx1.Text = list[1].ToString();
textBx2.Text = list[2].ToString();
textBx3.Text = list[3].Tostring();
etc.
// I can't create textbox dynamically as I need the text box to be placed in specific places in the form . How do I do it the best way?
}
}
Run Code Online (Sandbox Code Playgroud)
听起来像是Controls.Find()的工作.您可以动态构建字符串并使用该名称搜索TextBox:
var textBox = this.Controls.Find("textBx" + i, true) as TextBox;
textBox.Text = list[i].ToString();
Run Code Online (Sandbox Code Playgroud)
这有点难看,因为它取决于TextBoxes的命名约定.也许更好的解决方案是在循环之前缓存TextBox的列表:
var textBoxes = new[] { textBx1, textBx2, textBx3 };
Run Code Online (Sandbox Code Playgroud)
然后你可以简单地索引到数组:
textBoxes[i].Text = list[i].ToString();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2891 次 |
| 最近记录: |