Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 label, 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 achieve this, here is code which I am trying-

C#
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();
    }
}

Thank you every body in advance
Posted

1 solution

1. store an array of labels, just like you do with an array of CheckBoxes and create the labels at the same time you create checkboxes

C#
label= new Label[bran_count];

for (int i = 0; i < bran_count; ++i)
{
    label[i] = new Label();
    ....     
    groupBox1.Controls.Add(checkBox[i]);
    groupBox1.Controls.Add(label[i]);
    .......          
}


2. In your event handler hide the label associated with unchecked checkbox
C#
int label_index = 0;
for(int i = 0; i < checkbox.Length; i++)
{
    if(checkbox[i].Name == ((CheckBox)sender).Name)
    {
         label_index = i;
         break;
    }
}

//add logic to check if checkbox is checked or not
//instead of removing the label just hide it
label[label_index].Visible = false;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900