#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0

    How to remove unchecked checkbox dynamically


    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-
    C# Code:
     
    <div class="bbcode_container">
    	<div class="bbcode_description">Code:</div>
    	<pre class="bbcode_code"style="height:372px;">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(); 
    } 
    }</pre>
    </div>


    Thank you in advance for any help
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    15
    Rep Power
    0
    hi dude

    Here is a solution for You there is an alternate coading for you

    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



    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]);
    .......
    }

IMN logo majestic logo threadwatch logo seochat tools logo