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