Click here to Skip to main content
14,641,699 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello Everybody
How can store name of dynamically created checkbox in a String array when I don't know how many checkbox will user select at runtime.
Say I have 10 dynamic checkboxes and out of 10 user select 6 checkboxes randomly now how can get the name of those selected checkboxes and store them in a String array.

I know how to use event handler on dynamic check box but confused how to declare Straing array when I don't know what will be be size of an array.

Here what I have done till now -
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;
                aCommand3 = new OleDbCommand("select * from batch_tbl where batch_branch LIKE '" + 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 the groupbox with batch code by generating dynamic checkboxes
                for (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();
                    batchCheckBox[j].Location = new System.Drawing.Point(104 * position, 30);
                    batchCheckBox[j].CheckStateChanged += new System.EventHandler(BatchBoxCheckedChanged);
                position = 1;
                gpposition += 100;
                this.Controls.RemoveByKey("lbl" + c.Name);
        int total_batch = 1;
        string[] batchname;
        private void BatchBoxCheckedChanged(object sender, EventArgs e)
            CheckBox batchBox = (CheckBox)sender;
            //Here I want to store name of checkbox in array
            if (batchBox.Checked == true)
                batchname = new String[total_batch];


1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Rather than string array, use List(of T)[^] generic class, which provide functionality for search, sort and manipulate list. Follow the link to find an example.
vishal deb 13-May-13 3:29am
Hi Maciej
I never used list but I think it I have to do something like this
<pre lang="c#">List<checkbox> chks = Controls.OfType<checkbox>().ToList();</pre>
But after this how can I get the name of batchBox (dynamic checkbox) name and store them in list. Can you give some code snippet
Maciej Los 13-May-13 3:44am
Declare List<checkbox> checkedList = new List<checkbox>(); as a global variable, then add a checked CheckBox to a checkedList inside BatchBoxCheckedChanged procedure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100