label1.Text = file2.ReadIni("Items", "Itm" + 1.ToString(), "");
            label2.Text = file2.ReadIni("Items", "Itm" + 2.ToString(), "");
            label3.Text = file2.ReadIni("Items", "Itm" + 3.ToString(), "");
            label4.Text = file2.ReadIni("Items", "Itm" + 4.ToString(), "");
            label5.Text = file2.ReadIni("Items", "Itm" + 5.ToString(), "");
我想用一个循环去代替,该如何写?我的写法如下,但调试不通过不知道为什么,           for (int i = 1; i <= 5; i++)
           {
             string s = "label" + i;
             Label c = this.Controls[s] as Label;
             c.Text = file2.ReadIni("Items", "Itm" + i.ToString(), "");
           }调试的时候 提示最后一行“未将对象引用设置到对象的实例。”
希望高手能赐教
谢谢!!!!

解决方案 »

  1.   


    Controls从0开始,修改为this.Controls[s-1]
      

  2.   

     for (int i = 1; i <= 5; i++)
               {
                 string s = "label" + i;
                 Label c = this.FindControl(s) as Label;
                 c.Text = file2.ReadIni("Items", "Itm" + i.ToString(), "");
               }
      

  3.   

    未将对象设置到对象的实例1、检查c是否实例化。 if(c!=null) {}
    2、检查file2是否实例化。
      

  4.   

    label5是不是在其他的pannle或groupBox里面??
    如果在groupBox1里:
    Label c = this.Controls[s] as Label;             ---------------》》》》
    Label c = groupBox1.Controls[s] as Label;
      

  5.   


    private List<Label> lb = new List<Label>();foreach (Control control in this.Controls)
                {
                    if (control.GetType().ToString() == "System.Windows.Forms.Label")
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            if (control.Name == "label" + (i + 1).ToString())
                            {
                                lb.Add((Label)control);
                            }
                        }
                    }
                }for(int i=0;i<5;i++)
    {
         lb[i].Text= file2.ReadIni("Items", "Itm" + (i+1).ToString(), "");}
      

  6.   

    上面webFORM的写法winform
    Label c = this.Controls(s) as Label;
      

  7.   

    Label c = this.Controls[s] as Label;
      

  8.   

    汗,没仔细看。
    LZ的label应该不是直接放在form里的,找找,应该是放在别的容器里了
      

  9.   

    干嘛要用循环呢?不理解。。就多打几行字嘛。你这样很dirty,以后有好日子过
      

  10.   

    不是这样的 这个得用循环的 东西不能写死的 以后客户只要修改INI文件就可以了
      

  11.   

    我将LZ的代码测试了下,如果Label就是放在窗体上面不是放在别的容器里面,那么代码是可以通过的现在报错就可能是你的Label放在别的容器里面了,
    那么你的Label c = this.Controls[s] as Label;
    要将this换成你装Label 的容器名
      

  12.   

    对啊 前四个在groupBox1里 label5在groupBox2里面,这情况应该咋写
      

  13.   

    没有放在同一个groupBox1中 在一个循环里能写出来么
      

  14.   

    readini 是什么类中的方法?呵呵
      

  15.   

               for (int i = 1; i <= 5; i++)
               {
                 string s = "label" + i;
                 //Label c = this.Controls[s] as Label;
                 Label c = this.Controls.Find(s,true)[0] as Label;
                 c.Text = file2.ReadIni("Items", "Itm" + i.ToString(), "");
               }换成上面的代码就OK了
      

  16.   

    file2是什么类中实例化来的?楼下的大大们
      

  17.   


    这位楼主的方法就可以啊;不行就用数组:
    Label[] lab = new Label[]{label1,label2,label3,label4,label5};
    for(int i =0;i< 5; i++)
    {
                lab[0].Text = file2.ReadIni("Items", "Itm" + (i+1).ToString(), "");
                lab[1].Text = file2.ReadIni("Items", "Itm" + (i+1).ToString(), "");
                lab[2].Text = file2.ReadIni("Items", "Itm" + (i+1).ToString(), "");
                lab[3].Text = file2.ReadIni("Items", "Itm" + (i+1).ToString(), "");
                lab[4].Text = file2.ReadIni("Items", "Itm" + (i+1).ToString(), "");
    }
      

  18.   

    as label之后要先判断结果是不是Null,如果是null,需要continue一下。
      

  19.   

    既然这样
    你的Label也要动态添加才行啊
      

  20.   


    是我写的一个类的实例 系统好像也有INI文件操作的类吧 你要感兴趣我可以发给你 呵呵!!
    刚想调试代码结果客户来找我调试东西去啦一直没时间看帖子!!