Dynamic CheckBoxes

Posted on 2005-04-06
Last Modified: 2010-04-16

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?
Question by:praveenuni
    LVL 21

    Assisted Solution

    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

    LVL 6

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now