title: C# Using a form to load other user controls and have access to a base control or property
body: <p>Currently I have a C# program with a windows form and then a user control template put onto the form.  The user control template is really just used as a placeholder.  I have a series of other controls which inherit from this user control template.  </p>
  
  <p>Each of those controls have navigation buttons like 'Continue' and 'Back' on them and each control knows which control needs to be loaded next.  However what I need to figure out is an easier way to have variables that are global to these controls. </p>
  
  <p>The only workaround I have is that I pass the form to each control when they are loaded and use variables inside of the form to read and write to.  What would be the proper way to have each of these user control screens be built off of a base control which contained objects all of the controls could get to?  </p>
  
  <p>Sorry for the rambling nature of the post but I've been thinking about this problem all morning.  </p>
  
  <p>Here is some of the code:</p>
  
  <p>Most of what I have written was based on hiding and showing the user controls so that content in the controls wouldn't be lost during navigation.  I won't be needing to do that as eventually it will be loading the fields of data from a database.</p>
  
  <p>Code for initially loading control from form click:</p>
  
  <pre><code>        conTemplate1.Controls.Clear();
          conInbound Inbound = new conInbound(this);
          Inbound.Dock = DockStyle.Fill;
          Inbound.Anchor = (AnchorStyles.Left | AnchorStyles.Top);
          conTemplate1.Controls.Add(Inbound);
  </code></pre>
  
  <p>Code for Continue button inside of one of the controls:</p>
  
  <pre><code>    if ((Parent.Controls.Count - 1) &lt;= Parent.Controls.IndexOf(this))
          {
              UserControl nextControl = new conPartialClear();
              nextControl.Dock = DockStyle.Fill;
              Parent.Controls.Add(nextControl);
              this.Hide();
              Parent.Controls[Parent.Controls.IndexOf(this) + 1].Show();
          }
          else
          {
              this.Hide();
              Parent.Controls[Parent.Controls.IndexOf(this) + 1].Show();
          }
  </code></pre>