请问各位动态生成CheckBox如何实现呢.
 数据是由数据库中调用实现.
以下代码可以将数据库中的数据取出并显示在groupBox容器中.,.int y = 30,x=20;
try
{
    if (dt.Rows.Count != 0)
    {
        foreach (DataRow dr in dt.Rows)
        {            CheckBox chkbox = new CheckBox();
            chkbox.Location = new Point(x, y);
            chkbox.Name = "chk"+dr["Pid"].ToString();
            chkbox.Text = dr["Pname"].ToString();
            groupBox2.Controls.Add(chkbox);
            if (x == 540)
            {
                x = 20;
                y += 30;
            }
            else
            {
                x += 130;
            }
        }
    }
}
catch { }
现在的问题是点击button后,怎么获取这些checkbox选中的值呢.,.
 麻烦解释得详尽点....

解决方案 »

  1.   

    groupBox2.FindControl("chk"+dr["Pid"].ToString()) as CheckBox;
      

  2.   

    foreach(CheckBox cb in groupBox2.Controls)
    {
       if(cb.GetType().Name = "CheckBox")
       {
          //循环取
           if(cb.Checked)
          {
             //..........
          }
       }
    }
      

  3.   

    既然都放到groupBox2里面看   可以foreach(Control control groupBox2。Controls)
    {
       if( control is checkbox)
       {
        .......
       }
    }
      

  4.   

    foreach(CheckBox cb in groupBox2.Controls)
    {
       if(cb.GetType().Name = "CheckBox")
       {
          //循环取
           if(cb.Checked)
          {
             //..........
          }
       }
    }
    正解,找控件,根据值来选择~
      

  5.   


    int y = 30,x=20;
    try
    {
        if (dt.Rows.Count != 0)
        {
            foreach (DataRow dr in dt.Rows)
            {            CheckBox chkbox = new CheckBox();
                chkbox.Location = new Point(x, y);
                chkbox.Name = "chk"+dr["Pid"].ToString();
                chkbox.Text = dr["Pname"].ToString();
                chkbox.CheckedChanged += new EventHandler(chkbox_CheckedChanged);  //加个事件
                groupBox2.Controls.Add(chkbox);
                if (x == 540)
                {
                    x = 20;
                    y += 30;
                }
                else
                {
                    x += 130;
                }
            }
        }
    }
    catch { }
    private void chkbox_CheckedChanged(object sender,EventArgs e)
    {
       //((CheckBox)sender)---->   就是当前点击的CheckBox
    }
      

  6.   

    1.保存一个CheckBox数组或用ArrayList之类的集合存储这些CheckBox,需要的时候取出来用。
    2.枚举GroupBox中的控件,判断是否是CheckBox,如是,进一步判断其Text属性的值,确定是否是需要的。
      

  7.   

    用一个arraylist把所有的CHECKBOX的NAME保存下来。
    foreach(control in groupbox.controls)
    {
    if(control.name == arrraylist[i].tostring() && control.gettype().tostring() == "CheckBox")
    {
    //dosth
    }
    i++
    }