winfrom动态创建CheckBox 如图我想在winfrom动态的创建CheckBox(选择和全选之类的就不用每个控件都去写代码),那位前辈能提供相关的代码呢?谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 全选:private void button1_Click(object sender, EventArgs e) { foreach (Control c in this.Controls) { if (c is CheckBox) { (c as CheckBox).Checked = true;//全不选只要为false } } } 楼上的写法,会把全选及反选都算进去了。LZ可以试试CheckedListBox控件试试,或者:将所有需要选择的CheckBox放在某个Panel里,再使用楼上的方法。 动态创建CheckBoxprivate void button1_Click(object sender, EventArgs e){ for (int i = 0; i < 33; i++) { CheckBox chk = new CheckBox(); if (i == 31) chk.Text = "全选"; else if (i == 32) chk.Text = "不选"; else chk.Text = (i+1).ToString().PadLeft(2, '0'); chk.Location = new Point(i % 12 * 50, i / 12 * 30); chk.Width = 50; chk.Name = "checkBox" + i; this.Controls.Add(chk); }} 窗体加一个panel private void Form1_Load(object sender, EventArgs e) { for (int i = 0; i < 5; i++) { CheckBox box = new CheckBox(); box.Location = new Point(i * 40, 10); box.Width = 30; box.Text = i.ToString(); panel1.Controls.Add(box); } CheckBox chk = new CheckBox(); chk.Location = new Point(20, 60); chk.Width = 50; chk.Text = "全选"; chk.CheckedChanged += new EventHandler(chk_CheckedChanged); this.Controls.Add(chk); } void chk_CheckedChanged(object sender, EventArgs e) { foreach (Control ctrl in panel1.Controls) { if (ctrl is CheckBox) ((CheckBox)ctrl).Checked = ((CheckBox)sender).Checked; } } 使用CheckedListBox比较好,this.checkedListBox1.ColumnWidth = 40;this.checkedListBox1.MultiColumn = true;for (int i = 0; i < 100; i++){ this.checkedListBox1.Items.Add(i.ToString().PadLeft(2));}如果一定要使用CheckBox,这样:for (int i = 0; i < 30; i++){ CheckBox cb = new CheckBox(); cb.Text = i.ToString().PadLeft(2); cb.Name = "myCheckBox" + i.ToString().PadLeft(2); cb.Left = ...; //这里调整位置 cb.Top = ...; this.Controls.Add(cb);} private void button4_Click(object sender, EventArgs e){ for (int i = 0; i < 33; i++) { CheckBox chk = new CheckBox(); if (i == 31) chk.Text = "全选"; else if (i == 32) chk.Text = "不选"; else chk.Text = (i + 1).ToString().PadLeft(2, '0'); chk.Location = new Point(i % 12 * 50, i / 12 * 30); chk.Width = 50; chk.Name = "checkBox" + i; chk.CheckedChanged += new EventHandler(checkBox_CheckedChanged); this.Controls.Add(chk); }}private void checkBox_CheckedChanged(object sender, EventArgs e){ switch ((sender as CheckBox).Text) { case "全选": // 可以实现全选和不选的功能 不选那一个CheckBox可以去掉 { for (int i = 0; i < 31; i++) { (this.Controls["checkBox" + i] as CheckBox).Checked = (sender as CheckBox).Checked; } break; } default: break; // 其它情况在本行处理 }} checkboxlistfor (int i = 0; i < 4; i++){ CheckBox chk= new CheckBox(); chk.Top = 10; chk.Text = i.ToString(); chk.Left = i * 30; chk.AutoSize = true; chk.CheckedChanged += new EventHandler(chk_CheckedChanged); Controls.Add(chk);}void chk_CheckedChanged(object sender, EventArgs e) { CheckBox chk=sender as CheckBox ; } 无法启动服务。服务进程无法连接到服务控制器上。 Treeview的用法 bindingsource 更新 DataGridView排序后自定义属性失效 关于一个类似dropdownlist的控件 执行到TcpListener.AcceptSocket()就不继续执行了 有没有比indexof效率高的字符串比较方法? wince应用程序出现严重错误,必须被关闭 好烦啊!散分! 请问 c#中 如何 将int,DateTime 变量 转为字符串型,反之呢? C#数组边界问题 c# 读取串口问题,怎么全部读取出来
private void button1_Click(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{
if (c is CheckBox)
{
(c as CheckBox).Checked = true;//全不选只要为false
}
}
}
楼上的写法,会把全选及反选都算进去了。
LZ可以试试CheckedListBox控件试试,
或者:将所有需要选择的CheckBox放在某个Panel里,再使用楼上的方法。
{
for (int i = 0; i < 33; i++)
{
CheckBox chk = new CheckBox();
if (i == 31) chk.Text = "全选";
else if (i == 32) chk.Text = "不选";
else chk.Text = (i+1).ToString().PadLeft(2, '0');
chk.Location = new Point(i % 12 * 50, i / 12 * 30);
chk.Width = 50;
chk.Name = "checkBox" + i;
this.Controls.Add(chk);
}
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
CheckBox box = new CheckBox();
box.Location = new Point(i * 40, 10);
box.Width = 30;
box.Text = i.ToString();
panel1.Controls.Add(box);
}
CheckBox chk = new CheckBox();
chk.Location = new Point(20, 60);
chk.Width = 50;
chk.Text = "全选";
chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
this.Controls.Add(chk);
} void chk_CheckedChanged(object sender, EventArgs e)
{
foreach (Control ctrl in panel1.Controls)
{
if (ctrl is CheckBox) ((CheckBox)ctrl).Checked = ((CheckBox)sender).Checked;
}
}
this.checkedListBox1.ColumnWidth = 40;
this.checkedListBox1.MultiColumn = true;
for (int i = 0; i < 100; i++)
{
this.checkedListBox1.Items.Add(i.ToString().PadLeft(2));
}
如果一定要使用CheckBox,这样:
for (int i = 0; i < 30; i++)
{
CheckBox cb = new CheckBox();
cb.Text = i.ToString().PadLeft(2);
cb.Name = "myCheckBox" + i.ToString().PadLeft(2);
cb.Left = ...; //这里调整位置
cb.Top = ...;
this.Controls.Add(cb);
}
private void button4_Click(object sender, EventArgs e)
{
for (int i = 0; i < 33; i++)
{
CheckBox chk = new CheckBox();
if (i == 31) chk.Text = "全选";
else if (i == 32) chk.Text = "不选";
else chk.Text = (i + 1).ToString().PadLeft(2, '0');
chk.Location = new Point(i % 12 * 50, i / 12 * 30);
chk.Width = 50;
chk.Name = "checkBox" + i;
chk.CheckedChanged += new EventHandler(checkBox_CheckedChanged);
this.Controls.Add(chk);
}
}private void checkBox_CheckedChanged(object sender, EventArgs e)
{
switch ((sender as CheckBox).Text)
{
case "全选": // 可以实现全选和不选的功能 不选那一个CheckBox可以去掉
{
for (int i = 0; i < 31; i++)
{
(this.Controls["checkBox" + i] as CheckBox).Checked = (sender as CheckBox).Checked;
}
break;
}
default: break; // 其它情况在本行处理
}
}
for (int i = 0; i < 4; i++)
{
CheckBox chk= new CheckBox();
chk.Top = 10;
chk.Text = i.ToString();
chk.Left = i * 30;
chk.AutoSize = true;
chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
Controls.Add(chk);
}
void chk_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk=sender as CheckBox ;
}