[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Dynamic CheckBoxes

Hello,

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.LayersPanel.Controls.Add(oCheckBox);
}

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?
0
praveenuni
Asked:
praveenuni
2 Solutions
 
YurichCommented:
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;
   }

   else
   {
        // whatever you want to do for other check boxes
   }
}

hope it helps

hope it helps
           

0
 
TDSnetCommented:
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;
    this.LayersPanel.Controls.Add(chk);
    x++;
}

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.

Cheers,

Jack Pick
Total Data Solutions
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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