Thomas C
Thomas C

Reputation: 52

Accessing dynamically created checkbox in c#

I am creating a few checkboxes when I open a form with the following code:

    private void OpenFolder_Load(object sender, EventArgs e)
    {
        int i = 0;
        foreach (string file in filesToOpen)
        {
            Label lbl = new Label();
            lbl.Text = Path.GetFileNameWithoutExtension(file);
            lbl.Width = 200;
            lbl.Height = 25;
            lbl.AutoEllipsis = true;
            lbl.Location = new System.Drawing.Point(10, 40 + 25 * i);

            this.Controls.Add(lbl);

            string checkName = "check" + i;
            CheckBox check = new CheckBox();
            check.Checked = true;
            check.AccessibleName = checkName;
            check.Location = new System.Drawing.Point(340, 40 + 25 * i);
            check.CheckedChanged +=new EventHandler(check_CheckedChanged);

            this.Controls.Add(check);

            CheckBoxes.Add(check);

            i++;
        }

and I am trying to check the state of the checkboxes everytime one changes to toggle my OK button (the user can validate only if there are a certain number of the checkboxes checked)

here is the code I use, but it fails as I am not able to target the checkboxes:

    private void check_CheckedChanged(Object sender, EventArgs e)
    {
        for (int i = 0; i < filesToOpen.Count(); i++)
        {
            string tbarName = "tbar" + i;
            string checkName = "check" + i;

            CheckBox ckb = this.Controls.OfType<CheckBox>()
                     .Where(c => c.AccessibleName.Equals(checkName)) as CheckBox;
            TrackBar tkb = this.Controls.OfType<TrackBar>()
                     .Where(t => t.AccessibleName.Equals(tbarName)) as TrackBar;
            //TrackBar tkb = this.Controls.Find(tbarName, false).First() as TrackBar;
            //CheckBox ckb = this.Controls.Find(checkName, false).First() as CheckBox;

            if (ckb.Checked == true)
            {
                //do stuff
            }
        }
    }

what am I doing wrong/really wrong?

Upvotes: 0

Views: 3626

Answers (3)

Artak
Artak

Reputation: 2887

Iterating over all the checkboxes with every check is not required and is readlly hard processing work. Instead when creating you always know in what state you've created those - so just keep the count of "Checked" checkboxes. When a checkbox being checked increment the count, and when one unchecked - take out 1 from the count. And later have a check: "if (count == requiredCount) {//Logic here}" So the code will look like:

private int checkedCount;
private void check_CheckedChanged(Object sender, EventArgs e) 
{
  this.checkedCount += (sender as CheckBox).Checked?1:-1;
  if(this.checkedCount == requiredCount)
  { 
    //do stuff 
  } 
}

Good luck with development.

Upvotes: 0

ChrisF
ChrisF

Reputation: 137128

Given that you add the checkboxes to your own list:

CheckBoxes.Add(check);

it would be simpler to loop over that rather than trying to find the control associated with the file:

foreach (var checkBox in CheckBoxes)
{
    if (checkbox.Checked)
    {
        // Do stuff...
    }
}

However, you shouldn't need to use a separate list. This line is wrong:

CheckBox ckb = this.Controls.OfType<CheckBox>()
                   .Where(c => c.AccessibleName.Equals(checkName)) as CheckBox;

Where returns a IEnumerable<CheckBox> but you are trying to cast it directly to a CheckBox which will return null. What you should have is:

CheckBox ckb = this.Controls.OfType<CheckBox>()
                   .Where(c => c.AccessibleName.Equals(checkName)).First();

You will still need to check to see if ckb is null (just in case there is nothing on the list) but this should return you the control you are looking for.

Upvotes: 1

Antony Koch
Antony Koch

Reputation: 2053

Check the type of "this" and then check its Controls collection - your checkboxes are probably a few iterations down the tree.

You'd need some kind of recursive find controls function such as the one found in this article

Upvotes: 0

Related Questions