title: Add/Remove panel controls by clicking button on dynamically added usercontrol
body: <p>I am new to c# windows forms and I'm trying to make a GUI that has two panels. The left panel is used to display dynamically added user controls that contain buttons. The right panel is used to display dynamically added usercontrols that contain other controls: textboxes, labels, comboboxes, buttons, etc. </p>
  
  <p>I've created a form1 that has the two panels. I can successfully load both panels with different UserControl.cs content by using a menu that I've added to the top of the form. When I click a menu option, form1 buttonPanel is loaded with the appropriate buttonPanel.cs content and form1 mainPanel is loaded with the appropriate mainPanel.cs content. However, when I click the button that exists on buttonPanel.cs, I can't get form1 mainPanel to change it's content. </p>
  
  <p>ie: WelcomeMenu.cs has a button called btnPage2 that should change mainPanel controls to show Page2.cs usercontrol instead of Welcome.cs usercontrol. </p>
  
  <p>I want to be able to use in the button click handler on UserControl.cs: </p>
  
  <pre><code>mainPanel.Controls.Clear();
  UserControl usrCtl = new UserControl();
  Form1.mainPanel.Controls.Add(usrCtl);
  </code></pre>
  
  <p>My problem seems to be that WelcomeMenu.cs cannot see or access Form1 mainPanel.</p>
  
  <p>Is there a way to make this work? Or am I trying to do this the wrong way?</p>
  
  <p>My reason for this method is so I can load a new buttonPanel.cs usercontrol and mainPanel.cs usercontrol for each department and then be able to change mainPanel content for each button I click in the current buttonPanel. I'm trying to avoid creating a bunch of panels on Form1 and then hiding them and only making them visible when I need them.</p>
  
  <p>Update:
  buttonMenu.cs</p>
  
  <p>{</p>
  
  <pre><code>{
   public partial class ucWelcomeMenu : UserControl
  
      public ucWelcomeMenu()
      {
          InitializeComponent();
      }
  
      private void btnPage2_Click(object sender, EventArgs e)
      {
          Form1.mainPanel.Controls.Clear();
          ucWelcome frm = new ucWelcome();
          Form1.mainPanel.Controls.Add(frm);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Form1.mainPanel.Controls.Add(frm) generates an error on Form1.mainPanel that states: 
  "An object reference is required for the non-static field, method, or property 'Form1.mainPanel'</p>
  
  <p>UPDATE 2:</p>
  
  <p>Ok. I've searched several different links and found some helpful information. However, I am still unable to fire an event from a button click in a dynamically added UserControl.cs. I have 2 panels on Form1. menuPanel and mainPanel. They are both set to Modifiers = Public.</p>
  
  <p>Here is my Form1:</p>
  
  <pre><code>namespace TestUserControl
  
  public partial class Form1 : Form
  {
      private ucWelcomeMenu welc = new ucWelcomeMenu();
  
      public Form1()
      {
          InitializeComponent();
  
          ucWelcomeMenu welcomeMenu = new ucWelcomeMenu();
          menuPanel.Controls.Add(welcomeMenu);
  
          welc.ButtonClick += new EventHandler(this.CustomEvent_Handler);
      }
  
      private void CustomEvent_Handler(object sender, EventArgs e)
      {
          MessageBox.Show("Yes");
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And Here is my UserControl:</p>
  
  <pre><code>namespace TestUserControl.UserControls
  
  public partial class ucWelcomeMenu : UserControl
  {
      public event EventHandler ButtonClick;
  
      public ucWelcomeMenu()
      {
          InitializeComponent();
      }
  
      private void btnPage2_Click(object sender, EventArgs e)
      {
          if (ButtonClick != null)
              ButtonClick(sender, e);
      }
  }
  </code></pre>
  
  <p>}</p>