I am trying to create groupbox dynamically and these GB will create on selection on checkbox.
Means I have 5 check boxes now if I select 1st CB then 1 GB with some other dynamic checkbox shall be created, if I select 3rd check then another GB shall be created with few more other checkboxes.
For this I am trying with this code whee I am able to create dynamic checkboxes for fixed groupbox which is already created at design time.
My scenario is - 5 Branches have multiple batches. Now user will choose branch from dynamically checkboxes and on that basis btaches will be displayed in groupbox for each branch.
Branch1 Branch2 Branch3 Branch4 Branch5 if user select 3rd and 5th branch then GB1 will show Branch3's Batches and GB2 shall show Branch5's Batches
Here is the code -
//Check Box for Branches
private void RO_SelectedIndexChanged(object sender, EventArgs e)
String m = RO.SelectedItem.ToString();
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();
ds2.Tables.Constraints.Add("pk_bno", ds2.Tables.Columns, true);
int bran_count = ds2.Tables.Rows.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.Rows[i].ToString();
checkBox[i].Location = new System.Drawing.Point(125 * i, 15);
checkBox[i].CheckStateChanged += new System.EventHandler(CheckBoxCheckedChanged);
int count = 1;
int position = 1;
//Code for handling event when branch check box is selected or unselected
private void CheckBoxCheckedChanged(object sender, EventArgs e)
CheckBox c = (CheckBox)sender;
String str = null;
if (c.Checked == true)
str = c.Text;
aCommand3 = new OleDbCommand("select * from batch_tbl where batch_branch LIKE '" + str + "'", main_connection);
aAdapter3 = new OleDbDataAdapter(aCommand3);
ds3 = new DataSet();
ds3.Tables.Constraints.Add("pk_bno", ds3.Tables.Columns, true);
int batch_count = ds3.Tables.Rows.Count;
//filling the groupbox with batch code by generating dynamic checkboxes
for (int i = 0; i < batch_count; ++i)
checkBox[i] = new CheckBox();
checkBox[i].Name = "check" + Convert.ToString(i);
checkBox[i].Text = ds3.Tables.Rows[i].ToString();
checkBox[i].Location = new System.Drawing.Point(104*position, 30);
groupBox2.Text = c.Text;
this.Controls.RemoveByKey("lbl" + c.Name);
This code very fine but I don't know how many Branch CB will use select, so how can I put GB for each selected branch at desig time, for his I need to genrated GB at runtime on selection of Branch CheckBoxes.
Just to make sure I understand. How many groupboxes do you want to display at one time? It sounds like anywhere from 2 to 6. 1 for the branch checkboxes and possibly one each(up to 5) for the batches of each branch. If this true, I would suggest adding and placing all the groupboxes, with enough room in each one for the maximum number of checkboxes you might add, in the design window. then according to whether the checkbox corresponding to a particular groupbox is checked, set visible to true or false. Then, whenever a branch checkbox is unchecked, clear the checkboxes for that particular groupbox, and whenever it's checked re-load the checkboxes from the database.
For guidance. Yes you are right, but my problem is that I dont know how many branch checkbox user will select, he/she might select 1 or all or 3 out of five. In future if branch increases then check box will for branches will also increase. So, it is not feasiable to add groupboxes for each branches at design time. It should be at run time.
Means groupbox(s) should be generated at runtime as per selection by user for branches.
there can be 8 to 10 batches for each branch, means 8 to 10 checkboxs for each groupboxes. And yes there will be 3 combobox and 1 more checkbox inside each group box.
combobox1 for-> Teachers name coming from table
combobox2 for-> Duration of session coming from table (1 hour, 1.3 hours, 2 hours)
combobox3 for-> Time slot (8:00Am, 8:30 AM, 9:00 AM, 9:30 AM, 10:00 AM, 10:30 AM.. till 8:00 PM)
checkbox for-> Days (S,M,T,W,Th,F,S)