如图我想在winfrom动态的创建CheckBox(选择和全选之类的就不用每个控件都去写代码),那位前辈能提供相关的代码呢?谢谢!

解决方案 »

  1.   

    全选:
    private void button1_Click(object sender, EventArgs e)
            {
                foreach (Control c in this.Controls)
                {
                    if (c is CheckBox)
                    {
                        (c as CheckBox).Checked = true;//全不选只要为false
                    }
                }
            }
      

  2.   


    楼上的写法,会把全选反选都算进去了。
    LZ可以试试CheckedListBox控件试试,
    或者:将所有需要选择的CheckBox放在某个Panel里,再使用楼上的方法。
      

  3.   

    动态创建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);
        }
    }
      

  4.   

    窗体加一个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;
                }
            }
      

  5.   

    使用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);
    }
      

  6.   


    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;   // 其它情况在本行处理
        }
    }
      

  7.   

    checkboxlist
    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 ;
              
            }