Changing the properties of a dynamically created control
<p>I have a Form with a <code>LayoutPanel</code> that has dynamically added <code>buttons</code> inside. The <code>buttons</code> are added at runtime, which works, but my problem is I'd like to set the properties to one of the <code>buttons</code> to disabled if a <code>textBox</code> is empty and enable it when it the <code>textBox</code> is not empty.</p>

<p>Here is a code example, with the error I am receiving below:</p>

<pre><code>private void Form1_Load(object sender, EventArgs e)
{

    // Create 3 buttons

    Button button1 = new Button();
    button1.Name = "button1";
    tableLayoutPanel1.Controls.Add(button1, 0, 0);

    Button button2 = new Button();
    button1.Name = "button2";
    tableLayoutPanel1.Controls.Add(button2, 0, 0);

    Button button3 = new Button();
    button3.Name = "button1";
    tableLayoutPanel1.Controls.Add(button3, 0, 0);
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(textBox1.Text))
    {
        button1.Enabled = false;
    }
    else
    {
        button1.Enabled = true;
    }
}
</code></pre>

<blockquote>
  <p>CS0103 The name 'button1' does not exist in the current context</p>
</blockquote>

<p>Should I be declaring the buttons elsewhere so the entire code can see that they do in fact exist or is my problem elsewhere? Thanks.</p>