C#中如果动态循环生成控件数组

formatkm 2003-08-30 07:55:30
我在c#中要生成10个textBox,应该怎样写循环代码,命名?谢谢
...全文
312 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
formatkm 2003-08-30
  • 打赏
  • 举报
回复
非常感谢,问题解决了!
formatkm 2003-08-30
  • 打赏
  • 举报
回复
哦,I see!是我的循环那超出数组的范围了。谢谢。
formatkm 2003-08-30
  • 打赏
  • 举报
回复
tb[i].Location = new System.Drawing.Point(nLeft, i*nHeight + nTop);
这句报的错误:
“其他信息: 索引超出了数组界限。”
saucer 2003-08-30
  • 打赏
  • 举报
回复
class YourForm : Form
{
private System.Windows.Forms.TextBox[] textboxes;

....
int n = 10;
int nLeft = 10;
int nTop = 10;
int nHeight = 30;
textboxes = new TextBox[n];
for (int i=0; i < n; i++)
{
textboxes[i] = new TextBox();
textboxes[i].Name = "txt" + i.ToString();
textboxes[i].Location = new System.Drawing.Point(nLeft, i*nHeight + nTop);
this.Controls.Add(textboxes[i]);
}
formatkm 2003-08-30
  • 打赏
  • 举报
回复
我的是在winform中
saucer 2003-08-30
  • 打赏
  • 举报
回复
winform? webform?

for(int i=0; i < 10; i++)
{
TextBox tb = new TextBox();
...
this.Controls.Add(tb); //for webform, you need to do Form1.Controls.Add(tb);
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧