Click here to Skip to main content
Click here to Skip to main content

Single Instance Children Forms in MDI Applications

, 24 May 2004
Rate this:
Please Sign up or sign in to vote.
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 Poke tongue | ;-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

Share

About the Author

Polis Pilavas
Web Developer
Cyprus Cyprus
Simpsons' fanatic.

Comments and Discussions

 
AnswerAnother way to resolve this scenario PinmemberKalaimani V19-Nov-09 3:26 
GeneralA shorter solution (maybe) PinmemberCaio Proiete5-Jul-09 15:46 
GeneralSolution with C++ (even shorter than c#) ; P PinmemberLemontee20-Jan-09 2:35 
Generalsingle mdichild form PinmemberCRISTI_LUPUL22-May-08 22:14 
GeneralThis is how I did it Pinmemberdenzeo26-Oct-07 17:39 
QuestionIsn't this shorter? Logically, no need of boolean Pinmemberzinczinc26-Apr-07 14:16 
AnswerRe: Isn't this shorter? Logically, no need of boolean PinmemberRAGolko29-Nov-07 2:49 
GeneralShort Code Pinmemberhkjadav5-Apr-07 9:21 
GeneralHi Pinmemberpannujagwinder15-Sep-06 11:20 
Generaltry this Pinmembershabonaa4-Mar-06 3:57 
Questionhow to pass data from chil to parent Pinmemberserfin9-Nov-05 7:26 
AnswerRe: how to pass data from chil to parent PinmemberPolis Pilavas21-Nov-05 9:13 
AnswerRe: Why not disable the "form" button/menu item? Pinmemberleonleslie5-Dec-04 10:57 
GeneralSingleton Pattern PinmemberDave Veeneman5-Jun-04 15:56 
GeneralI am using next code Pinmemberakorolev104-Jun-04 22:47 
GeneralSuggestion... PinmemberDavid M. Kean25-May-04 3:21 
GeneralPatterns Pinmemberuasking25-May-04 3:07 
QuestionWhy not disable the "form" button/menu item? PinprotectorMarc Clifton25-May-04 2:43 
AnswerRe: Why not disable the "form" button/menu item? PinmemberPolis Pilavas27-May-04 22:09 

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.

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