Hello
I am making a win form application on frame work 3.5 using C#.
I am creating dynamic checkboxes depending on the numbers of records in access table. After this I am also able to get the name, text and other properties of dynamically created checkboxes and I am displaying the name of selected checkboexs in dynamically created labels on Form.
My problem is if I uncheck the checkbox, then also name of that checkbox is coming on lable, I want to remove that dynamic label from the form for which checkbox is unchecked.
Example of Form Design
dCeck1 dCheck2 dCheck3 dCheck4 dCheck5 (if dCeck1 dCheck2 dCheck4 dCheck5 is selected)
dLabel1 dLabe2 dLabe4 dLable5 (these label will be displayed)
Now if I unselect dCheck4 then dLabel4 should be removed.

How can I achive this, here is code which I am trying-

private void RO_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox1.Controls.Clear();
            String m = RO.SelectedItem.ToString();

            Console.WriteLine(m);

            aCommand2 = new OleDbCommand("select * from branch_tbl,region_tbl where branch_tbl.region_id=region_tbl.region_id and region_tbl.region_name LIKE '"+m +"'", main_connection);
            aAdapter2 = new OleDbDataAdapter(aCommand2);
            ds2 = new DataSet();
            aAdapter2.Fill(ds2, "app_info");
            ds2.Tables[0].Constraints.Add("pk_bno", ds2.Tables[0].Columns[0], true);

            int bran_count = ds2.Tables[0].Rows.Count;
            Console.WriteLine(bran_count);

            checkBox = new System.Windows.Forms.CheckBox[bran_count];

            for (int i = 0; i < bran_count; ++i)
            {
                checkBox[i] = new CheckBox();
                checkBox[i].Name = "radio" + Convert.ToString(i);
                checkBox[i].Text = ds2.Tables[0].Rows[i][2].ToString();

                checkBox[i].Location = new System.Drawing.Point(125 * i, 15);
                groupBox1.Controls.Add(checkBox[i]);
                checkBox[i].CheckStateChanged += new System.EventHandler(CheckBoxCheckedChanged);
            }

        }
        int count = 1;
        int position = 1;
        private void CheckBoxCheckedChanged(object sender, EventArgs e)
        {
            CheckBox c = (CheckBox)sender;
            Label myLabel;
            if (c.Checked == true)
            {
                count++;
                myLabel = new Label();
                myLabel.Name="label" + count.ToString();
                myLabel.Text = c.Text;
                myLabel.Location = new Point(20,65*position);
                this.Controls.Add(myLabel);
                position++;
            }
            else
            {
                return;
            }
            if(c.CheckState == CheckState.Unchecked)
            {
                this.Controls.Remove(myLabel);
                this.Update();
            }
        }  

Thankyou in advance to all

Using a number to identify the label, means that it will be hard to recognize which label goes with which checkbox. By changing the format of the label name to include the name of the checkbox, this becomes much easier. Something like this should work:

private void CheckBoxCheckedChanged(object sender, EventArgs e)
{
    CheckBox c = (CheckBox)sender;
    Label myLabel = new Label();
    if (c.Checked == true)
    {
        myLabel.Name="lbl" + c.Name;
        myLabel.Text = c.Text;
        myLabel.Location = new Point(20,65*position);
        this.Controls.Add(myLabel);
        position++;
    }
    else
    {
        this.Controls.RemoveByKey("lbl" + c.Name);
        this.Update();
    }
} 

I noticed, in your code, that if a user keeps checking and unchecking the checkboxes, more labels will get added, without consolidating space when any are removed. By using the number of the checkbox to set the location of the label, the label for each checkbox will only appear in one place.

myLabel.Location = new Point(20,65*Int32.Parse(c.Name.Substring(5)));

Edited 3 Years Ago by tinstaafl

This is really nice code to make your website dynamic. I like your logic very much. I definate use this code to my website. And its result also so impressive. I like its result.

This article has been dead for over six months. Start a new discussion instead.