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.
I have been doing this the following way:
- create a parent form with "isMdiContainer" set to "true"
- create a menu strip in in the mdi container
- create a child form implementing singleton in order for the form to be shown only once
- Add something like the following code to the mdi container:
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();
}
}
}
Now, this obviously works fine. But it is a a pain to maintain. Everytime I want to add another child form I have to:
- implement Singleton
- create the corresponding property in the mdi container
- get the instance and set the mdi parent
- Hide the new form when other buttons are clicked
- Show the form and hide all other Forms when the corresponding button is clicked
Whats a more elegant or lets say efficient approach to archive this?