• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

Dynamic CheckBoxes


I 'm creating checkboxes dynamically like this:

foreach (string Name in this.NameList)
CheckBox oCheckBox = new CheckBox();
oCheckBox.Text = Name;
oCheckBox.AutoPostBack = true;
oCheckBox.CheckedChanged +=new EventHandler(oCheckBox_CheckedChanged);

This will create checkboxes list = count of names in the arraylist.
Problem: If I select first checkbox, it should automatically select all other checkboxes in the list. But not the same case if I select any other checkbox. How can I do this?
2 Solutions
i suggest you still want to have one handler for your checkboxes and still use only one foreach loop...

in this case in your handler you can try the following:

private void oCheckBox_CheckedChanged( object sender, System.EventArgs e )
   if((( CheckBox )sender ).Name == this.NameList[ 0 ])
      // i presume you have only one set of checkboxes, otherwise you'll have to use
      // tags to differentiate them
      foreach( Control c in this.Controls )
          if( c is CheckBox ) // check all checkboxes including the first one (doesn't matter)
               (( CheckedBox )c ).Checked = true;

        // whatever you want to do for other check boxes

hope it helps

hope it helps

Hi Praveenuni,

The only way I see of doing this is to create a UserControl based on the CheckBox control and then add THAT to your form dynamically.  The reason for this is that to make all the other CheckBoxes select when you check the first one you need to override the CheckBox_OnCheckChanged event.  This is the only way you will be able to make something else happen on the form besides the single CheckBox being checked.

So, add a new item to your project and select UserControl.  Change the Class declaration statement to read:

public class TDSCheckBox : System.Windows.Forms.CheckBox

Then, under the InitializeComponent method create a new method by typing:

protected override void OnCheckedChanged(EventArgs e)
    base.OnCheckedChanged (e);
    bool chkd = this.Checked;
    if (this.Tag.ToString() == "0")
        foreach (Control con in this.Parent.Controls)
            if (con.GetType().ToString() == "TDS.TDSCheckBox")
                TDSCheckBox chk = (TDSCheckBox)con;
                chk.Checked = chkd;

Obviously you can declare it as whatever you like (simply change TDSCheckBox to your own name) and you can also use your own Namespace (the default will be the name of the project ie. WindowsApplication1.TDSCheckBox)

Then just do as you were doing with the standard CheckBox control in your application.

foreach (String name in this.NameList)
    int x = 0;
    TDSCheckBox chk = new TDSCheckBox();
    chk.Tag = x.ToString();
    chk.Text = name;

And that's it.  You need not worry about the line which adds the CheckedChanged EventHandler.  The variable (int x) will be a count of the number of CheckBoxes and will be stored in the Tag property of each Control.  This is then checked when the Control is clicked and if the Tag property = "0" (ie. It is the first Control in the list) then all the other CheckBoxes will be Checked or Unchecked.

Hope this helps.


Jack Pick
Total Data Solutions
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now