日期:2014-05-20  浏览次数:20731 次

有10个Label控件,如何动态地给每个Label控件赋值,代码如下,请指点
能不能通过一个循环,给每个控件赋值

for(int   i=1;i <=10;i++)
{
    label   +   i   + ".Text "   =   i.toString();
}

------解决方案--------------------
信 誉 值: 75
1.做成数组
2.遍历Controls
------解决方案--------------------
在VB里,可以用 For...Each...Next 遍历form里的控件
不知对你有用不
------解决方案--------------------
for (int i = 1; i <= 10; i++)
{
Control[] vControls = Controls.Find(string.Format( "label{0} ", i), true);
if (vControls.Length > 0)
foreach (Control vLabel in vControls)
((Label)vLabel).Text = i.ToString();
}

------解决方案--------------------
asp.net用FindControl(string name)
------解决方案--------------------
foreach (Control ctl in this.Controls)
{
if(ctl.GetType().ToString() == "System.Windows.Forms.Label ")
{
((Label)ctl).Text = "mylabel ";
MessageBox.Show( "ok,label is "+ctl.Text);
}

}
------解决方案--------------------
foreach (Control ctl in this.Controls)
{
if(ctl.GetType().ToString() == "System.Windows.Forms.Label ")
{
((Label)ctl).Text = "mylabel ";
MessageBox.Show( "ok,label is "+ctl.Text);
}

}

为什么要这样呀?

if(ctl is System.Windows.Forms.Label)

就可以了嘛