The Artima Developer Community
Sponsored Link

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

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Roy Osherove

Posts: 1807
Nickname: royo
Registered: Sep, 2003

Roy Osherove is a .Net consultant based in Israel
Problem & Solution: Allow only one instance of any MDI child form in your MDI application Posted: Jun 16, 2004 3:40 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Roy Osherove.
Original Post: Problem & Solution: Allow only one instance of any MDI child form in your MDI application
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Roy Osherove
Latest Posts From ISerializable

Advertisement

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

            }

      }

}

 

 

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

Topic: The Lunar Men Previous Topic   Next Topic Topic: One Tablet Pen Down

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use