title: How to Get Data From User Control on Form
body: <p>I'm building a simple app in Visual Studio 2010 using C#, and I'm doing this to get familiar with user control because it seems to make it easier than creating multiple forms.</p>
  
  <p>On the main form, there is a dropdown list the contains 2 values, "<code>UCType1</code>" and "<code>UCType2</code>". I created 2 different user controls. I was using panel on the main form to display the user control according to what they selected in the dropdown list.</p>
  
  <p>I was able to display the appropriate user control based on the user selection, but now I ran into some issue. I couldn't get the main form to read data from the user control.</p>
  
  <p>Let's say there is a button "<code>Execute</code>" and a label "<code>Warning</code>" on the main form. There is a textbox "<code>Name</code>" on the user control.</p>
  
  <p>The scenario is: when the user hit "<code>Execute</code>", if "<code>Name</code>" is blank, "<code>Warning</code>" will display some error message.</p>
  
  <p>I was trying to use <code>if(UserControl1.Name.Text == "")</code> on the main form but I couldn't even reference it like that.</p>
  
  <p>I know I can just create separate forms to make it easier since that will make all variable in the same file, but I want to know if there's a way to do it with user control because I would like to get familiar with it.</p>
  
  <p>Thanks</p>
  
  <p>This is how I display my user control</p>
  
  <pre><code>private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
  {
      UCType1 uc1 = new UCType1();
      panel1.Controls.Clear();
      panel1.Controls.Add(uc1);
  }
  </code></pre>
  
  <p>and when I was trying to display the data from user control</p>
  
  <pre><code>private void executeButton_Click(object sender, EventArgs e)
  {
      UCType1 uc1 = new UCType1();
      warning.Text = uc1.Name;
  }
  </code></pre>
  
  <p>nothing was happening.</p>