Hi All

I am trying to generated the dynamic labels depending on the checked checkboxes which are dynamically created.

My .Net frame is 3.5
Here is code for dynamic checkboxes

System.Windows.Forms.CheckBox[] 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);
                checkBox[i].CheckStateChanged += new System.EventHandler(CheckBoxCheckedChanged);

        private void CheckBoxCheckedChanged(object sender, EventArgs e)

Any guidance is welcome, if possible give some code snippet.

Cast your sender to a checkbox and see if your array contains that checkbox...

private void CheckBoxChanged(object sender, EventArgs e)
    CheckBox selectedBox = sender as CheckBox;

    if(selectedBox == null)
        // Somehow a different control type called this method.

        // Do what you need to do if your array contains the selected checkbox

You will need to include: using System.Linq; at the top of your code file.

If you're creating the table based on the status of the different checkboxes, you probably don't need to handle the .CheckStateChanged event. I assume the checkboxes are being used to set the columns of the table. If so, merely loop through the array and if the checked property is true add the column to the table, or add the info to your query string.

This article has been dead for over six months. Start a new discussion instead.