c# - How to store name of dynamic checkbox in an String array -


how can store name of dynamically created checkbox in string array when don't know how many checkbox user select @ runtime. have 10 dynamic checkboxes , out of 10 user select 6 checkboxes randomly how can name of selected checkboxes , store them in string array.

i know how use event handler on dynamic check box confused how declare straing array when don't know be size of array.

here have done till -

    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;             batchcheckbox = new checkbox[batch_count];             //filling groupbox batch code generating dynamic checkboxes             (int j=0; j < batch_count; ++j)             {                 batchcheckbox[j] = new checkbox();                 batchcheckbox[j].name = "batch" + convert.tostring(k);                 batchcheckbox[j].text = ds3.tables[0].rows[j][1].tostring();                 console.writeline(batchcheckbox[j].text);                 batchcheckbox[j].location = new system.drawing.point(104 * position, 30);                 gpbox[gpcount].controls.add(batchcheckbox[j]);                 batchcheckbox[j].checkstatechanged += new system.eventhandler(batchboxcheckedchanged);                 position++;                 count++;                 console.writeline(batchcheckbox[j].name);                 k++;             }             position = 1;             gpposition += 100;         }         else         {             count--;             this.controls.removebykey("lbl" + c.name);             this.update();         }     }     int total_batch = 1;     string[] batchname;     private void batchboxcheckedchanged(object sender, eventargs e)     {         checkbox batchbox = (checkbox)sender;         //here want store name of checkbox in array         if (batchbox.checked == true)         {             batchname = new string[total_batch];             total_batch++;          }         else         {         }     } 

you can try this:

    //gets checkbox's on form     list<checkbox> chks = controls.oftype<checkbox>().tolist();      //take checked, , select name property     list<string> names = chks.where(c => c.checked).select(c => c.name).tolist(); 

update

for testing print list of selected names:

string txt = ""; foreach(string name in names) {    txt += name+" \n\r"; } messagebox.show(txt); 

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 -