title: How do I find and use a programatically-created control?
body: <p>I've created a number of buttons on a form based on database entries, and they work just fine. Here's the code for creating them. As you can see I've given them a tag:</p>
  
  <pre><code>for (int i = 0; i &lt;= count &amp;&amp; i &lt; 3; i++)
  {
      btnAdd.Text = dataTable.Rows[i]["deviceDescription"].ToString();
      btnAdd.Location = new Point(x, y);
      btnAdd.Tag = i;
  
      this.Controls.Add(btnAdd);
  }
  </code></pre>
  
  <p>I use these buttons for visualising a polling system. For example, I want the button to be green when everything is fine, and red when something is wrong. </p>
  
  <p>So the problem I'm running into is referencing the buttons later so that I can change their properties. I've tried stuff like the following:</p>
  
  <pre><code>this.Invoke((MethodInvoker)delegate
  {
      // txtOutput1.Text = (result[4] == 0x00 ? "HIGH" : "LOW"); // runs on UI thread
      Button foundButton = (Button)Controls.Find(buttonNumber.ToString(), true)[0];
      if (result[4] == 0x00)
      {
          foundButton.BackColor = Color.Green;
      }
      else
      {
          foundButton.BackColor = Color.Red;
      }
  });
  </code></pre>
  
  <p>But to no avail... I've tried changing around the syntax of <code>Controls.Find()</code> but still have had no luck. Has anyone encountered this problem before or know what to do?</p>