title: C# user control gets loaded 2 times instead of once
body: <p>I'm working on a  tray icon application that when pressed opens a form that has a couple of panels and in one of the panels there are user controls that i switch on and off with buttons</p>
  
  <pre><code>    Form1 form = new Form1();
      private void Icon_Click(object sender, EventArgs e)
      {
          form.Left = Cursor.Position.X - (form.Width / 2);
          form.Top = Screen.PrimaryScreen.WorkingArea.Bottom - form.Height;
  
          Form fc = Application.OpenForms["Form1"];
          try
          {
              if (fc == null)
              {
                  form.Left = Cursor.Position.X - (form.Width / 2);
                  form.Top = Screen.PrimaryScreen.WorkingArea.Bottom - form.Height;
                  form.Show();
              }
              else
              {
                  fc.Close();
              }
              form = new Form1();
          }
          catch (Exception)
          {
          }
      }
  </code></pre>
  
  <p>This is the code for showing and hiding the form</p>
  
  <p><a href="https://i.stack.imgur.com/Xfzq7.png" rel="nofollow noreferrer">This is how the application looks so far</a></p>
  
  <p>As u can see in the image the user control part is in the middle of the 2 other panels and i switch the user controls with the buttons in the bottom panel</p>
  
  <p>The application uses a TcpListener that is in one of the user controls, once started it listens on a port constantly, but since the user control is loaded twice i get <code>Only one usage of each socket address is normally permitted</code> exception and the TcpListener just stops listening because of this.
  I've loaded the user controls to the form from the toolbox and all of them are set to <code>visible = false</code> from the properties window, and i show the user control when the form loads in the Form1() function.</p>
  
  <p>This is the code that controls the showing/hiding of user controls and switching the background image of the buttons(color)</p>
  
  <pre><code>if (lastbutton != null)
  {
        lastbutton.BackgroundImage = idleimages[lastbutton];
        lastcontrol.Visible = false;
        lastcontrol.Enabled = false;
  }
  lastbutton = mycomputerbtn;
  lastbutton.BackgroundImage = activeimages[lastbutton];
  lastcontrol = myComputerScreen1;
  lastcontrol.Enabled = true;
  lastcontrol.Visible = true;
  lastcontrol.Focus();
  </code></pre>
  
  <p>I don't know if this should be happening but i set a messagebox in the user control and that message box is shown when i open the form in the visual studio designer</p>