title: WPF how do I create a textbox dynamically and find the textbox on a button click?
body: <p>I am creating a <code>TextBox</code> and a <code>Button</code> dynamically using the following code:</p>
  
  <pre><code>Button btnClickMe = new Button();
  btnClickMe.Content = "Click Me";
  btnClickMe.Name = "btnClickMe";
  btnClickMe.Click += new RoutedEventHandler(this.CallMeClick);
  
  someStackPanel.Childern.Add(btnClickMe);
  
  TextBox txtNumber = new TextBox();
  txtNumber.Name = "txtNumber";
  txtNumber.Text = "1776";
  
  someStackPanel.Childern.Add(txtNumber);
  </code></pre>
  
  <p>I hook up to a click event to the <code>Click Me</code> button.  The click me button even is fired correctly.  However I cannot find the <code>TextBox</code> I entered dynamically.</p>
  
  <p>Here is my click me event:</p>
  
  <pre><code>protected void ClickMeClick(object sender, RoutedEventArgs e)
  {
      // Find the phone number
      TextBox txtNumber = this.someStackPanel.FindName("txtNumber") as TextBox;
  
      if (txtNumber != null)
      {
          string message = string.Format("The number is {0}", txtNumber.Text);
  
          MessageBox.Show(message);    
      }
      else
      {
          MessageBox.Show("Textbox is null");
      }
  }
  </code></pre>
  
  <p>How can I find the <code>TextBox</code> <code>txtNumber</code>?</p>