Geeks With Blogs
Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done

Original Article:  http://weblogs.asp.net/rosherove/archive/2004/06/16/156933.aspx

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

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());

            }

      }

}

 

 

Posted on Tuesday, September 5, 2006 10:21 AM WinForms | Back to top


Comments on this post: Allow only one instance of any MDI child form in your MDI application

# re: Allow only one instance of any MDI child form in your MDI application
Requesting Gravatar...
Can you provide the same code in VB.NET.

Thanks
KP
Left by KP on Oct 18, 2007 2:31 AM

# re: Allow only one instance of any MDI child form in your MDI application
Requesting Gravatar...
I found a simplier solution by checking to see if your form name exists in MdiChildren, if so you just activate the mdi child instead of creating a new instance.

Private Sub ClientToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClientToolStripMenuItem.Click
For Each mdiChild As Form In Me.MdiChildren
If mdiChild.Name = "ClientConfig" Then
mdiChild.Activate()
Exit Sub
End If
Next
Dim frmClientConfig As New ClientConfig
frmClientConfig.MdiParent = Me
frmClientConfig.Show()
End Sub
Left by Dave on Nov 28, 2007 9:39 PM

Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net