title: c# main menu and mdi forms
body: <p>So far in my life, as a .net developer, I have made heavy use of mdi forms to display particular "menu points" such as for instance "module 1" "module 2" and so on.</p>
  
  <p>I have been doing this the following way:</p>
  
  <ul>
  <li>create a parent form with "isMdiContainer" set to "true"</li>
  <li>create a menu strip in in the mdi container</li>
  <li>create a child form implementing singleton in order for the form to be shown only once</li>
  <li>Add something like the following code to the mdi container:</li>
  </ul>
  
  <hr>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  
  namespace Myapp
  {
  
  public partial class MdiContainer : Form
  {
      private module1 Module1Window;
      private module2 Module2Window;
      private module3 Module3Window;
  
      public FormContainer()
      {
          InitializeComponent();
          this.Module1Window = modul1.getInstance();
          this.Module1Window.MdiParent = this;
          this.Module2Window = modul2.getInstance();
          this.Module2Window.MdiParent = this;
          this.Module3Window = modul3.getInstance();
          this.Module3Window.MdiParent = this;
          this.Module1Window.Show();
      }
      private void module1ToolStripMenuItem_Click(object sender, EventArgs e)
      {
          this.Module3Window.Hide();
          this.Module2Window.Hide();
          this.Module1Window.Show();
      }       
      private void module2ToolStripMenuItem_Click(object sender, EventArgs e)
      {
          this.Module1Window.Hide();
          this.Module3Window.Hide();
          this.Module2Window.Show();
      }
      private void module3ToolStripMenuItem_Click(object sender, EventArgs e)
      {
          this.Module1Window.Hide();
          this.Module2Window.Hide();
          this.Module3Window.Show();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Now, this obviously works fine. But it is a a pain to maintain. Everytime I want to add another child form I have to:</p>
  
  <ul>
  <li>implement Singleton</li>
  <li>create the corresponding property in the mdi container</li>
  <li>get the instance and set the mdi parent</li>
  <li>Hide the new form when other buttons are clicked</li>
  <li>Show the form and hide all other Forms when the corresponding button is clicked</li>
  </ul>
  
  <p>Whats a more elegant or lets say efficient approach to archive this?</p>