Click here to Skip to main content
6,291,722 members and growing! (15,126 online)
Email Password   helpLost your password?
Languages » C# » General     Beginner

Single Instance Children Forms in MDI Applications

By Polis Pilavas

An article on C# MDI applications.
C#, .NET, WinXP, Visual Studio, Dev
Posted:24 May 2004
Views:52,097
Bookmarked:25 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
13 votes for this article.
Popularity: 3.54 Rating: 3.18 out of 5
2 votes, 15.4%
1
2 votes, 15.4%
2
3 votes, 23.1%
3
1 vote, 7.7%
4
5 votes, 38.5%
5

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


Member
Simpsons' fanatic.
Occupation: Web Developer
Location: Cyprus Cyprus

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
GeneralSolution with C++ (even shorter than c#) ; P PinmemberLemontee2:35 20 Jan '09  
Generalsingle mdichild form PinmemberCRISTI_LUPUL22:14 22 May '08  
GeneralThis is how I did it Pinmemberdenzeo17:39 26 Oct '07  
QuestionIsn't this shorter? Logically, no need of boolean Pinmemberzinczinc14:16 26 Apr '07  
AnswerRe: Isn't this shorter? Logically, no need of boolean PinmemberRAGolko2:49 29 Nov '07  
GeneralShort Code Pinmemberhkjadav9:21 5 Apr '07  
GeneralHi Pinmemberpannujagwinder11:20 15 Sep '06  
Generaltry this Pinmembershabonaa3:57 4 Mar '06  
Questionhow to pass data from chil to parent Pinmemberserfin7:26 9 Nov '05  
AnswerRe: how to pass data from chil to parent PinmemberPolis Pilavas9:13 21 Nov '05  
GeneralRe: Why not disable the "form" button/menu item? Pinmemberleonleslie10:57 5 Dec '04  
GeneralSingleton Pattern PinmemberDave Veeneman15:56 5 Jun '04  
GeneralI am using next code Pinmemberakorolev1022:47 4 Jun '04  
GeneralSuggestion... PinmemberDavid M. Kean3:21 25 May '04  
GeneralPatterns Pinmemberuasking3:07 25 May '04  
GeneralWhy not disable the "form" button/menu item? PinsupporterMarc Clifton2:43 25 May '04  
GeneralRe: Why not disable the "form" button/menu item? PinmemberPolis Pilavas22:09 27 May '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 May 2004
Editor: Smitha Vijayan
Copyright 2004 by Polis Pilavas
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project