title: How can I get the array index of a given object in C# with control arrays?
body: <p>I am dynamically adding a bunch of controls to a form. Each control calls the same method, and in that method I need to know the array index of the the control that performed the action.</p>
  
  <pre><code>CheckBox[] myCB = new CheckBox[100];
  int i;
  for (i = 0; i &lt; 100; i++)
  {
      myCB[i] = new CheckBox();
      myCB[i].Text = "Clicky!";
      myCB[i].Click += new System.EventHandler(dynamicbutton_Click);
      tableLayoutPanel1.Controls.Add(myCB[i]);
  }
  
  private void dynamicbutton_Click(Object sender, System.EventArgs e)
  {
      label1.Text = sender.???array index property???.ToString();
  }
  </code></pre>
  
  <p>So if I click <code>myCB[42]</code> <code>label1</code> will read "42"  Of course, if there is an easier way to handle dynamic controls I'd appreciate pointers.</p>