---
title: "Problem & Solution: Allow only one instance of any MDI child form in your MDI application"
url: "https://osherove.com/blog/2004/6/16/problem-amp-solution-allow-only-one-instance-of-any-mdi-chil.md"
canonical: "https://osherove.com/blog/2004/6/16/problem-amp-solution-allow-only-one-instance-of-any-mdi-chil.html"
published: "2004-06-16T06:56:00"
updated: "2004-06-16T06:56:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "One of the things I needed to do at an MDI application I was involved with was the ability to only load a specific form once, and make sure it does not load again. That form could not be modal, but could only have one…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Problem & Solution: Allow only one instance of any MDI child form in your MDI application

By Roy Osherove June 16, 2004 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published June 16, 2004; technical details may be outdated.

One of the things I needed to do at an MDI application I was involved with was the ability to only load a specific form once, and make sure it does not load again. That form could not be modal, but could only have one instance running inside the MDI parent.  I came up with a rather elegant solution, if I do say so myself :)

_(Of course what usually happens when I come up with an “elegant” solution is that someone shows me a simple property on the form that says something like "AllowOneInstanceOnly" which does exactly what I did here. )_

**How to call this class:**

In your MDI form, simply write this:

MdiFormLoader.LoadFormType(typeof(CustomerDetailsForm), this);

---

using System;

using System.Windows.Forms;

using System.Collections.Specialized;

namespace UI.Utilities

{

      public class MdiFormLoader

      {

            private static HybridDictionary m\_InitializedForms \= new HybridDictionary();

            public static void LoadFormType(Type formType, Form mdiParentForm)

            {

                  if (IsAlreadyLoaded(formType))

                  {

                        return;

                  }

                  FlagAsLoaded(formType);

                  Form frm \= (Form)Activator.CreateInstance(formType);

                  frm.MdiParent \= mdiParentForm;

                  frm.Closed += new EventHandler(FormClosed);

                  frm.Show();

            }

            private static void FlagAsLoaded(Type formType)

            {

                  m\_InitializedForms\[formType.Name\] \= true;

            }

            private static void FlagAsNotLoaded(Type formType)

            {

                  m\_InitializedForms\[formType.Name\] \= false;

            }

            private static bool IsAlreadyLoaded(Type formType)

            {

                  return ((m\_InitializedForms\[formType.Name\] != null) &&

                        (bool)m\_InitializedForms\[formType.Name\] \== true);

            }

            private static void FormClosed(object sender, EventArgs e)

            {

                  Form closingForm \= (Form)sender;

                  closingForm.Closed \-= new EventHandler(FormClosed);

                  FlagAsNotLoaded(sender.GetType());

            }

      }

}

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Jun 19, 2004

    ### [\[Article\] Simplified Database Unit testing using Enterprise Services](https://osherove.com/blog/2004/6/19/article-simplified-database-unit-testing-using-enterprise-se.html)

- Jun 16, 2004

    ### [Implementing the BackgroundWorker component - today(source included)](https://osherove.com/blog/2004/6/16/implementing-the-backgroundworker-component-todaysource-incl.html)

- Jun 13, 2004

    ### [Unit Testing in Whidbey - some pointers for improvement](https://osherove.com/blog/2004/6/13/unit-testing-in-whidbey-some-pointers-for-improvement.html)

- Jun 12, 2004

    ### [Writing a VS.Net Add-in, ugh](https://osherove.com/blog/2004/6/12/writing-a-vsnet-add-in-ugh.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
