先看下面代码:string[] purview1 = {"1","2","3","4","5"};
                    for (int i = 0; i < purview1.Length; i++)
                    {
                        foreach (Control p in PlaceHolder11.Controls)
                        {
                            CheckBox cb = (CheckBox)p;
                            if (cb.TabIndex.ToString() == purview1[i])
                            {
                                cb.Checked = true;
                            }
                            else
                            {
                                cb.Checked = false;
                            }
                        }
                    }//或者
string[] purview1 = {"1","2","3","4","5"};
                    foreach (Control p in PlaceHolder11.Controls)
                    {
                        for (int i = 0; i < purview1.Length; i++)
                        {
                            CheckBox cb = (CheckBox)p;
                            if (cb.TabIndex.ToString() == purview1[i])
                            {
                                cb.Checked = true;
                            }
                            else
                            {
                                cb.Checked = false;
                            }
                        }
                    }在PlaceHolder11容器控件里面有五个checkbox,checkbox的tabindex属性分别为1,2,3,4,5
我想通过上面的循环来对PlaceHolder11里面的checkbox的进行选择,
但上面两段代码运行之后得到的结果都是之后最后一个checkbox选中了,其他的前四个都没被选
找不出问题所在
请路过的各位大侠看看是什么问题,怎样解决???

解决方案 »

  1.   

    foreach (Control p in PlaceHolder11.Controls)
    {
         CheckBox cb = (CheckBox)p;                            
         cb.Checked = Array.Exists(purview1, c => c == cb.TabIndex.ToString());                           
    }
    首先这段代码简单点,你不用写那么复杂。第二建议你调试下看看TabIndex是多少
      

  2.   

    你单步调试就会发现,前四个都会执行到else的部分吧?就是说前四次都等于false了
      

  3.   

    改写一下,不知道是不是你要的意思:
    int[] purview1 = { 1, 2, 3, 4, 5 };
    for (int i = 0; i < PlaceHolder11.Controls.Count; i++)
    {
    CheckBox cb=PlaceHolder11.Controls[i] as CheckBox;
    cb.Checked=(cb.TabIndex == purview1[i]);
    }
      

  4.   

    把else 去掉前几个都正确执行了,不过每次都会将之前的操作覆盖所以只会保留最后一次
      

  5.   


    灰常感谢!!按照你的写,可以了
    想问一下
    Array.Exists(purview1, c => c == cb.TabIndex.ToString());
    这句是什么意思呢?
    “c => ”这个东东不懂
      

  6.   

    c=> Lambda 表达式