在循环中创建控件
我有一些代码可以将新单元格添加到表格中并用文本框填充它们。
到目前为止,我的编码方式运行良好:
TableCell tCell1 = new TableCell();
TableCell tCell2 = new TableCell();
TableCell tCell3 = new TableCell();
TableCell tCell4 = new TableCell();
TableCell tCell5 = new TableCell();
TableCell tCell6 = new TableCell();
TableCell tCell7 = new TableCell();
TextBox txt1 = new TextBox();
TextBox txt2 = new TextBox();
TextBox txt3 = new TextBox();
TextBox txt4 = new TextBox();
TextBox txt5 = new TextBox();
TextBox txt6 = new TextBox();
TextBox txt7 = new TextBox();
tCell1.Controls.Add(txt1);
tCell2.Controls.Add(txt2);
tCell3.Controls.Add(txt3);
tCell4.Controls.Add(txt4);
tCell5.Controls.Add(txt5);
tCell6.Controls.Add(txt6);
tCell7.Controls.Add(txt7);
tRow.Cells.Add(tCell1);
tRow.Cells.Add(tCell2);
tRow.Cells.Add(tCell3);
tRow.Cells.Add(tCell4);
tRow.Cells.Add(tCell5);
tRow.Cells.Add(tCell6);
tRow.Cells.Add(tCell7);
如您所见,基本上有 4 条指令重复了 7 次。 我确信必须有一种方法可以通过 FOR 循环中的 4 行代码来完成此任务,并动态分配所有名称,但我似乎找不到任何可以为我指明方向的东西做吧。
我所追求的是类似以下内容:
for (int i = 0; i < 6; i++)
{
TableCell tCell[i] = new TableCell();
TextBox txt[i] = new TextBox();
tCell[i].Controls.Add(txt[i]);
tRow.Cells.Add(tCell[i]);
}
任何帮助将不胜感激。
I have have some code which adds new cells to a table and fills them with text boxes.
The way I've coded it so far works fine:
TableCell tCell1 = new TableCell();
TableCell tCell2 = new TableCell();
TableCell tCell3 = new TableCell();
TableCell tCell4 = new TableCell();
TableCell tCell5 = new TableCell();
TableCell tCell6 = new TableCell();
TableCell tCell7 = new TableCell();
TextBox txt1 = new TextBox();
TextBox txt2 = new TextBox();
TextBox txt3 = new TextBox();
TextBox txt4 = new TextBox();
TextBox txt5 = new TextBox();
TextBox txt6 = new TextBox();
TextBox txt7 = new TextBox();
tCell1.Controls.Add(txt1);
tCell2.Controls.Add(txt2);
tCell3.Controls.Add(txt3);
tCell4.Controls.Add(txt4);
tCell5.Controls.Add(txt5);
tCell6.Controls.Add(txt6);
tCell7.Controls.Add(txt7);
tRow.Cells.Add(tCell1);
tRow.Cells.Add(tCell2);
tRow.Cells.Add(tCell3);
tRow.Cells.Add(tCell4);
tRow.Cells.Add(tCell5);
tRow.Cells.Add(tCell6);
tRow.Cells.Add(tCell7);
As you can see there's basically 4 instructions getting repeated 7 times. I'm sure there has to be a way to accomplish this with just 4 lines of code within a FOR loop and having all the names dynamically assigned but I just can't seem to find anything that would point me in the direction of how to do it.
Something like the following is what I'm after:
for (int i = 0; i < 6; i++)
{
TableCell tCell[i] = new TableCell();
TextBox txt[i] = new TextBox();
tCell[i].Controls.Add(txt[i]);
tRow.Cells.Add(tCell[i]);
}
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你写的实际上看起来很接近我。 不过,有几点需要牢记。
我不相信你需要数组索引。 只要 tRow 在循环外初始化,它每次都会添加新元素。 您可能还需要设置每个文本框的 ID 属性,以便您可以访问您可能正在寻找的任何特定文本框。
What you wrote actually looks pretty close to me. There a re a few points to keep in mind though.
I don't believe you need the array index. As long as tRow is initialized outside the loop it will add the new elements each time. You also may want to set the ID property of each textbox so you can access any specific on you may be looking for down the road.
感谢所有有用的答案。 对于那些问我用数组做什么的人来说,我没有! 这只是我想要实现的目标的一个例子。
Ian 和 Lars 的想法是正确的,因为我稍后需要引用这些文本框,所以我只需要使用 Eugene 和 Lubos 的解决方案,并确保我添加一行,为它们提供顺序 ID(txt1、txt2)等)这样我就可以做到这一点。
再次感谢所有精彩(且快速!)的输入,我现在爱上了这个网站!
Thanks for all the helpful answers. To those asking questions about what I was doing with the arrays, I wasn't! That was just an example of what I was trying to achieve.
Ian and Lars got the right idea in the fact that I will need to refer to these textboxes later so I just need to use Eugene and Lubos' solution and make sure I'm adding a line that will give them sequential ID's (txt1, txt2 etc) so that I can do this.
Thanks again for all the wonderful (and quick!) input, I'm now in love with this site!
我认为应该这样做:
确保将 6 更改为 7。
I think this should do it:
Make sure that 6 is changed to a 7.
这应该可以正常工作吗?
不过我不太明白你需要什么名字。
您是否计划使用“txt5”名称作为对该特定文本框的引用?
为什么不直接使用
tRow.Cells[4].Controls[0] As TextBox
?This should work fine?
I don't really get what you need the names for though.
Do you plan on using the "txt5" name as a reference to that specific textbox?
Why not just use
tRow.Cells[4].Controls[0] As TextBox
?