读书人

怎么获取flowLayoutPanel中所有选中ch

发布时间: 2013-09-07 14:12:45 作者: rapoo

如何获取flowLayoutPanel中所有选中checkbox的text?
本帖最后由 wjx515628 于 2013-09-05 01:59:24 编辑

  private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
Panel pan = new Panel();
pan.Width = 150;
pan.Height = 150;
pan.BackColor = System.Drawing.Color.Yellow;
flowLayoutPanel1.Controls.Add(pan);

PictureBox pic = new PictureBox();
pic.Width = 150;
pic.Height = 130;
Image O_Image = Image.FromStream(WebRequest.Create("http://www.baidu.com/img/baidu_logo.gif").GetResponse().GetResponseStream());
pic.Image = O_Image;
pic.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
pan.Controls.Add(pic);

CheckBox chk2 = new CheckBox();
chk2.Width = 70;
chk2.Text = "test" + i.ToString(); chk2.Checked = (i % 1 == 0); chk2.Visible = true;
chk2.Top = 130;
chk2.Width = 13;


pan.Controls.Add(chk2);

Label lab = new Label();
lab.Text = "lab" + i.ToString();
lab.Top = 130;
lab.Left = 15;
pan.Controls.Add(lab);

}



代码如上,checkbox放在pannel中 pannel放在flowLayoutPanel中,如何获取选中checkbox的文本?

帮忙看下这个全选和取消代码怎么不好用?flowLayoutPanel1.Controls是不是不能获取到panel中的控件?
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
{
CheckBox all = sender as CheckBox;
foreach (Control ctl in flowLayoutPanel1.Controls)
{
if (ctl is CheckBox)
{
CheckBox chk = ctl as CheckBox;
chk.Checked = all.Checked;
}
}
}
}


[解决办法]
全选全不选
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox all = sender as CheckBox;
foreach (Control ctl in flowLayoutPanel1.Controls)
{
if (ctl is Panel)
{
Panel p = ctl as Panel;
foreach (var ss in p.Controls)
{
if ((ss as CheckBox) is CheckBox)
{
if ((ss as CheckBox).Checked)
{
(ss as CheckBox).Checked = false;
}
else
{
(ss as CheckBox).Checked = true;


}
}
else
{

}
}
}
}
}

读书人网 >C#

热点推荐