title: Accessing dynamically created checkbox in c#
body: <p>I am creating a few checkboxes when I open a form with the following code:</p>
  
  <pre><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++;
          }
  </code></pre>
  
  <p>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)</p>
  
  <p>here is the code I use, but it fails as I am not able to target the checkboxes:</p>
  
  <pre><code>    private void check_CheckedChanged(Object sender, EventArgs e)
      {
          for (int i = 0; i &lt; filesToOpen.Count(); i++)
          {
              string tbarName = "tbar" + i;
              string checkName = "check" + i;
  
              CheckBox ckb = this.Controls.OfType&lt;CheckBox&gt;()
                       .Where(c =&gt; c.AccessibleName.Equals(checkName)) as CheckBox;
              TrackBar tkb = this.Controls.OfType&lt;TrackBar&gt;()
                       .Where(t =&gt; 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
              }
          }
      }
  </code></pre>
  
  <p>what am I doing wrong/really wrong?</p>