我有10个LABLE,ID分别是LAB1,LAB2,LAB3,LAB4.....我现在要给10个LAB赋值。LABi=i*9-5!怎么用循环来赋值

解决方案 »

  1.   

    最简单的就是把这些lable都扔进集合,然后遍历集合赋值
      

  2.   

    for (int i = 1; i <= 10; i++)
    {
    Label[] lbl = (Label[])this.Controls.Find("LAB" + i.ToString(), true);
    if (lbl.Length == 1)
    {
    lbl[0].Text = (i * 9 - 5).ToString();
    }
    }
      

  3.   

    上面那个有点问题,以这个为准:
    for (int i = 1; i <= 10; i++)
    {
    Control[] ctls = this.Controls.Find("LAB" + i.ToString(), true);
    if (ctls.Length == 1 && (ctls[0] is Label))
    {
    (ctls[0] as Label).Text = (i * 9 - 5).ToString();
    }
    }
      

  4.   

    for (int i = 1; i <= 10; i++)
    {
        (this.Controls["LAB" + i.ToString()] as Label).Text = (i * 9 - 5).ToString();
    }
      

  5.   

    可能是我用的是.net2003,this.Controls没有find属性
      

  6.   

    this.Controls[int]内面只能INT,不能跟其他的
      

  7.   


    你先试过了再回复this.Controls["str"]其实里面的str也只是个索引值罢了,和C++里面的宏定义相似
      

  8.   

    错误提示:D:\qnyd\vote\vote.aspx.cs(215): 与“System.Web.UI.ControlCollection.this[int]”最匹配的重载方法具有一些无效参数
    D:\qnyd\vote\vote.aspx.cs(215): 参数“1” : 无法从“string”转换为“int”
      

  9.   

    public void FindControl(Control.ControlCollection  c) 
            { 
                foreach (System.Windows.Forms.Control control in c) 
                { 
                  if (control is Label) 
                  { 
                      Label l= control as Label; 
                      int i= int.Parse(Label.Id.ToString().Substring(3,1));
                      l.Text=(i*9-5).ToString(); 
                  } 
                } 
            }
      

  10.   

    for (int i = 1; i <= 10; i++)
    {
        Label lbl = this.Controls.Find("LAB" + i.ToString()) as Label;
        if (lbl!=null)
        {
            lbl[0].Text = (i * 9 - 5).ToString();
        }
    }
    this.Controls.Find()是方法,不是属性!
      

  11.   

    纠正:
    for (int i = 1; i <= 10; i++) 

        Label lbl = this.Controls.Find("LAB" + i.ToString()) as Label; 
        if (lbl!=null) 
        { 
            lbl.Text = (i * 9 - 5).ToString(); 
        } 
    } Find()是方法,不是属性!
      

  12.   

    this.Controls.Find在.NET2003中没有
      

  13.   

    for(int i=1;i<11;i++)
    {
      Label lbl = (Label)this.Controls.Find("LAB" + i.ToString());
      lb1.text=(i*9-5).toString();
    }
      

  14.   

    sorry,应该是FindControl方法:for (int i = 1; i <= 10; i++) 

        Label lbl = this.FindControl("LAB" + i.ToString()) as Label; 
        if (lbl!=null) 
        { 
            lbl.Text = (i * 9 - 5).ToString(); 
        }