日期:2014-05-17  浏览次数:20430 次

checkbox是否被选中的问题
在groupbox中动态生成了20个checkbox,当我的name值为1时第一个选中,name值为2时第二个选中,怎样做?

------解决方案--------------------
foreach遍历groupbox中的control:

C# code
int i = 0;
int index = 3;  //需要选中的checkbox
foreach(Control ctl in groupBox1.Controls)
{
  if(ctl is CheckBox)
  {
    i++;
    if(i == index) 
    {
        (ctl as CheckBox).Checked = true;
        break;
    }
  }
}

------解决方案--------------------
http://blog.sina.com.cn/s/blog_4cef5c7b0100ina6.html
------解决方案--------------------
生成时,CheckBox的tag中写入和name对应的值,这样遍历时,可以根据name的值和tag值对比找到 你要的CheckBox
如果web页,那就设置id或其他属性与name值对应,js遍历
------解决方案--------------------
比如
C# code

        private void SetCheck(string name)
        {
            foreach (Control c in groupBox1.Controls)
            {
                if (c is CheckBox)
                {
                    if (c.Tag.ToString() == name)
                    {
                        ((CheckBox)c).Checked = true;
                        break;
                    }
                }
            }
        }

------解决方案--------------------
我上面只是简单的写了个例子,你要用id匹配,可以这么写:
CheckBox chk = ctl as CheckBox;
if(chk.ID == yourID)
{
//...
}