c# windows 界面上有N个相同的控件,怎么历遍?

「已注销」 2009-04-02 06:38:31
N个文本框,这些文本框按照顺序赋值,赋值的内容是一个数组。。。

这个没办法设置循环吗?

我现在只能写N条语句。。一一赋值。
...全文
170 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
FenixeVon 2009-04-03
  • 打赏
  • 举报
回复
foreach(Control  c in this.Controls) 
{
int i=0;
if(c!=null && c is TextBox)
{
c.Text=array[i];
i++;
}
}

这个可以用
bulong0721 2009-04-03
  • 打赏
  • 举报
回复
还有一种办法在创建控件的时候就用数据绑定
POWER_WONG 2009-04-03
  • 打赏
  • 举报
回复
uping
lvjian2000 2009-04-03
  • 打赏
  • 举报
回复
呵呵,还有一种方式,根据Tag名称进行遍历。
leez0301 2009-04-03
  • 打赏
  • 举报
回复
for(int i = 0; i < this.Controls.Count; i++)
{
txtBox = this.Controls[i] as TextBox;
if(txtBox != null)
{
txtBox .Text = yourStrArray[i];
}
}

就这样了,楼上的很详细了
xmlbh 2009-04-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yonglovemeng 的回复:]
foreach(Control c in this.Controls)
{
int i=0;
if(c!=null && c is TextBox)
{
c.Text=array[i];
i++;
}
}
[/Quote]

同意!
torcher 2009-04-03
  • 打赏
  • 举报
回复
要try catch吧,不进行异常处理很容易出错。
wolf1118baby 2009-04-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 amandag 的回复:]
C# codestring[] array;//对array实例化并赋值TextBox t;for(inti=0; i<this.Controls.Count; i++)
{
t=this.Controls[i]asTextBox;if(t!=null)
{
t=yourArray[i];
}
}
[/Quote]
正解
018 2009-04-03
  • 打赏
  • 举报
回复
顺序赋值,可能你还得注意一下下面的代码(自动生成的):
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.textBox3);
dell0923 2009-04-03
  • 打赏
  • 举报
回复
麻烦你们测试下可行?一段错误的代码被copy了三次....
foreach(Control c in this.Controls)
{
int i=0;
if(c!=null && c is TextBox)
{
c.Text=array[i];
i++;
}
}

i一直是0?
  • 打赏
  • 举报
回复
up
冰凝瞬间1986 2009-04-03
  • 打赏
  • 举报
回复
foreach(Control c in this.Controls)
{
int i=0;
if(c!=null && c is TextBox)
{
c.Text=array[i];
i++;
}
}
wesleyluo 2009-04-03
  • 打赏
  • 举报
回复
就是楼上的代码,遍历赋值。
yonglovemeng 2009-04-02
  • 打赏
  • 举报
回复
foreach(Control c in this.Controls)
{
int i=0;
if(c!=null && c is TextBox)
{
c.Text=array[i];
i++;
}
}
li_dao_hang_1989 2009-04-02
  • 打赏
  • 举报
回复
foreach(Control c in this.Controls)
{
int i=0;
if(c!=null && c is TextBox)
{
c.Text=array[i];
i++;
}
}
加群asp.net交流83847699
amandag 2009-04-02
  • 打赏
  • 举报
回复
string [] array;
//对array实例化并赋值
TextBox t;
for(int i = 0; i < this.Controls.Count; i++)
{
t = this.Controls[i] as TextBox;
if(t != null)
{
t = yourArray[i];
}
}

111,126

社区成员

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

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

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