请问各位动态生成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选中的值呢.,.
麻烦解释得详尽点....
数据是由数据库中调用实现.
以下代码可以将数据库中的数据取出并显示在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选中的值呢.,.
麻烦解释得详尽点....
{
if(cb.GetType().Name = "CheckBox")
{
//循环取
if(cb.Checked)
{
//..........
}
}
}
{
if( control is checkbox)
{
.......
}
}
{
if(cb.GetType().Name = "CheckBox")
{
//循环取
if(cb.Checked)
{
//..........
}
}
}
正解,找控件,根据值来选择~
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
}
2.枚举GroupBox中的控件,判断是否是CheckBox,如是,进一步判断其Text属性的值,确定是否是需要的。
foreach(control in groupbox.controls)
{
if(control.name == arrraylist[i].tostring() && control.gettype().tostring() == "CheckBox")
{
//dosth
}
i++
}