Click here to Skip to main content
Licence 
First Posted 24 May 2004
Views 69,688
Bookmarked 33 times

Single Instance Children Forms in MDI Applications

By | 24 May 2004 | Article
An article on C# MDI applications.

Introduction

First of all, since this is my first article ever submitted (not only in CodeProject), let me just say that professionally I use older Microsoft technologies, and I have started studying and coding in C# just a couple of months ago. Therefore, I know that the method described here is most probably not the best solution but it served its cause for me. So, if you do have any comments or you can suggest a more efficient way of accomplishing this result, please let me know.

Most of the times, MDI applications are much more time-consuming than the normal SDI applications, and demand the maximum level of the developer's concentration (at least, this is the case with me ;p). This article (code snippet) will try to describe a very simple way of keeping only one instance of each MDI child form opened at all times, during the application's execution.

Background

I faced this problem during the development of a personal project (a dental solution) under C# and VS 2003. I had to make sure that whenever the user tries to open a new instance, the currently open instance will become active instead (or open if none is currently opened).

Using the code

The following snippet supposes that a toolbar exists on the MDI container form. Clicking on toolBarButton1 creates and displays the Patients form (our MDI child). All of the code goes into the click event of the toolbar:

private void toolBar1_ButtonClick(object sender, 
     System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
    // a flag to store if the child form is opened or not
    bool found = false;

    if (e.Button == toolBarButton1)
    {
       // get all of the MDI children in an array
       Form[] charr = this.MdiChildren;

       if (charr.Length == 0)      // no child form is opened
       {
          Patients myPatients = new Patients();
          myPatients.MdiParent = this;
          // The StartPosition property is essential
          // for the location property to work
          myPatients.StartPosition = FormStartPosition.Manual;
          myPatients.Location = new Point(0,0);
          myPatients.Show();
        }
        else      // child forms are opened
        {

          foreach (Form chform in charr)
          {
            if (chform.Name == "Patients")
            // one instance of the form is already opened
            {
              chform.Activate();
              found = true;
              break;   // exit loop
            }
            else
              found = false;      // make sure flag is set to
                                  // false if the form is not found
          }

          if (found == false)    
          {
            Patients myPatients = new Patients();
            myPatients.MdiParent = this;
            // The StartPosition property is essential
            // for the location property to work
            myPatients.StartPosition = FormStartPosition.Manual;
            myPatients.Location = new Point(0,0);
            myPatients.Show();
          }
        }
    }
}

History

Up to now, this is the exact solution I have implemented in my project. If I come up with any improvements, I will make sure I will post them. But in the meantime, if you can suggest anything cooler, please let me know.

Thanks.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Polis Pilavas

Web Developer

Cyprus Cyprus

Member

Simpsons' fanatic.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
AnswerAnother way to resolve this scenario PinmemberKalaimani V2:26 19 Nov '09  
GeneralA shorter solution (maybe) PinmemberCaio Proiete14:46 5 Jul '09  
GeneralSolution with C++ (even shorter than c#) ; P PinmemberLemontee1:35 20 Jan '09  
Generalsingle mdichild form PinmemberCRISTI_LUPUL21:14 22 May '08  
GeneralThis is how I did it Pinmemberdenzeo16:39 26 Oct '07  
QuestionIsn't this shorter? Logically, no need of boolean Pinmemberzinczinc13:16 26 Apr '07  
AnswerRe: Isn't this shorter? Logically, no need of boolean PinmemberRAGolko1:49 29 Nov '07  
GeneralShort Code Pinmemberhkjadav8:21 5 Apr '07  
private void toolBar1_ButtonClick(object sender,
      System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
      // a flag to store if the child form is opened or not
      bool found = false;
 
      if (e.Button == toolBarButton1)
      {
     // get all of the MDI children in an array
     Form[] charr = this.MdiChildren;
 
     if(charr.Length > 0)
     {
          for( int i = 0;i<charr.Length;i++)
          {
               // one instance of the form is already opened
               if(charr[i].Name.Equals("frmSChild"))
               {
                    charr[i].Activate();
                    found = true;
                    break;   // exit loop
               }
          }
     }
     else if(found == false)
     {
 
          frmSChild myPatients = new frmSChild();
          myPatients.MdiParent = this;
          // The StartPosition property is essential
          // for the location property to work
          myPatients.StartPosition = FormStartPosition.Manual;
          myPatients.Location = new Point(0,0);
          myPatients.Show();
     }
      }
}

 
Enjoy...Laugh | :laugh:
GeneralHi Pinmemberpannujagwinder10:20 15 Sep '06  
Generaltry this Pinmembershabonaa2:57 4 Mar '06  
Questionhow to pass data from chil to parent Pinmemberserfin6:26 9 Nov '05  
AnswerRe: how to pass data from chil to parent PinmemberPolis Pilavas8:13 21 Nov '05  
AnswerRe: Why not disable the "form" button/menu item? Pinmemberleonleslie9:57 5 Dec '04  
GeneralSingleton Pattern PinmemberDave Veeneman14:56 5 Jun '04  
GeneralI am using next code Pinmemberakorolev1021:47 4 Jun '04  
GeneralSuggestion... PinmemberDavid M. Kean2:21 25 May '04  
GeneralPatterns Pinmemberuasking2:07 25 May '04  
QuestionWhy not disable the "form" button/menu item? PinprotectorMarc Clifton1:43 25 May '04  
AnswerRe: Why not disable the "form" button/menu item? PinmemberPolis Pilavas21:09 27 May '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 25 May 2004
Article Copyright 2004 by Polis Pilavas
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid