2
votes

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?

1
If I have understood correctly: you are using MDIs to handle different forms - but only displaying one form at a time. Why?Dan Iveson
I don't think this will compile. Is it MdiContainer or FormContainer?Henk Holterman

1 Answers

1
votes

When do you create the menu items? Are these dynamically created along with the child forms?

If so what you can do is create the form and add it to a list and assign the menu item's Tag property to the form. Assign all the menu items click event to the same handler and within the handler do this...

private void menuStrip_Click(object sender, EventArgs e)
{
    var menu = (ToolStripItem)sender;
    var viewForm = (Form)sender.Tag;
    foreach(Form childForm in _childForms)
        childForm.Hide();
    viewForm.Show();
}

This same handler can be used no matter how many forms you have.

Alternatively you can have a key as the Tag and have a Dictionary<string, Form> so you can more lazily create the forms, however the concept is the same.