How create dynamic GroupBox in C# -


i trying create groupbox dynamically , these gb create on selection on checkbox.

means have 5 check boxes if select 1st cb 1 gb other dynamic checkbox shall created, if select 3rd check gb shall created few more other checkboxes. trying code whee able create dynamic checkboxes fixed groupbox created @ design time.

my scenario - 5 branches have multiple batches. user choose branch dynamically checkboxes , on basis btaches displayed in groupbox each branch. branch1 branch2 branch3 branch4 branch5 if user select 3rd , 5th branch gb1 show branch3's batches , gb2 shall show branch5's batches here code -

private void ro_selectedindexchanged(object sender, eventargs e)     {         groupbox1.controls.clear();         string m = ro.selecteditem.tostring();         console.writeline(m);         acommand2 = new oledbcommand("select * branch_tbl,region_tbl branch_tbl.region_id=region_tbl.region_id , region_tbl.region_name '"+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];         (int = 0; < 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;     //code handling event when branch check box selected or unselected     private void checkboxcheckedchanged(object sender, eventargs e)     {         checkbox c = (checkbox)sender;         //label mylabel;         string str = null;         if (c.checked == true)         {             str = c.text;                            acommand3 = new oledbcommand("select * batch_tbl batch_branch '" + str + "'", main_connection);             aadapter3 = new oledbdataadapter(acommand3);             ds3 = new dataset();             aadapter3.fill(ds3, "app_info");             ds3.tables[0].constraints.add("pk_bno", ds3.tables[0].columns[0], true);             int batch_count = ds3.tables[0].rows.count;             //filling groupbox batch code generating dynamic checkboxes             (int = 0; < batch_count; ++i)             {                 checkbox[i] = new checkbox();                 checkbox[i].name = "check" + convert.tostring(i);                 checkbox[i].text = ds3.tables[0].rows[i][1].tostring();                 console.writeline(checkbox[i].text);                 checkbox[i].location = new system.drawing.point(104*position, 30);                 groupbox2.text = c.text;                  groupbox2.controls.add(checkbox[i]);                 position++;                 count++;             }         }         else         {             count--;             this.controls.removebykey("lbl" + c.name);             this.update();         }     }  

this code fine don't know how many branch cb use select, how can put gb each selected branch @ desig time, need genrated gb @ runtime on selection of branch checkboxes.

i solved problem after thinking on problem. done have done generating dynamic checkboxes. here code

     private void ro_selectedindexchanged(object sender, eventargs e)     {         groupbox1.controls.clear();         string m = ro.selecteditem.tostring();         console.writeline(m);         acommand2 = new oledbcommand("select * branch_tbl,region_tbl branch_tbl.region_id=region_tbl.region_id , region_tbl.region_name '" + 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 checkbox[bran_count];          (int = 0; < 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);         }         gpbox=new groupbox[bran_count];     }    string str = null;    int count = 1;    int gpcount = 1;    int position = 1;    int gpposition = 110;     //code handling event when branch check box selected or unselected      private void checkboxcheckedchanged(object sender, eventargs e)     {         checkbox c = (checkbox)sender;         //label mylabel;         string str = null;         if (c.checked == true)         {             str = c.text;             gpbox[gpcount] = new groupbox();             gpbox[gpcount].name = "gpbox" + convert.tostring(count);             gpbox[gpcount].text = str;             gpbox[gpcount].location = new point(5, gpposition);             gpbox[gpcount].autosize = true;             this.controls.add(gpbox[gpcount]);              acommand3 = new oledbcommand("select * batch_tbl batch_branch '" + str + "'", main_connection);             aadapter3 = new oledbdataadapter(acommand3);             ds3 = new dataset();             aadapter3.fill(ds3, "app_info");             ds3.tables[0].constraints.add("pk_bno", ds3.tables[0].columns[0], true);             int batch_count = ds3.tables[0].rows.count;             //filling groupbox batch code generating dynamic checkboxes             (int = 0; < batch_count; ++i)             {                 checkbox[i] = new checkbox();                 checkbox[i].name = "check" + convert.tostring(i);                 checkbox[i].text = ds3.tables[0].rows[i][1].tostring();                 console.writeline(checkbox[i].text);                 checkbox[i].location = new system.drawing.point(104 * position, 30);                 gpbox[gpcount].controls.add(checkbox[i]);                 position++;                 count++;             }             position = 1;             gpposition += 100;         }         else         {             count--;             this.controls.removebykey("lbl" + c.name);             this.update();         }     } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -