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);
                groupBox1.Controls.Add(checkBox[i]);
                checkBox[i].CheckStateChanged += new System.EventHandler(CheckBoxCheckedChanged);

            }
        }
        private void CheckBoxCheckedChanged(object sender, EventArgs e)
        {
            //MessageBox.Show();
        }

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

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.
        return;
    }

    if(myCheckBoxArray.Contains(selectedBox))
    {
        // 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.