Click here to Skip to main content
15,867,771 members
Articles / Programming Languages / C#
Article

Single Instance Children Forms in MDI Applications

Rate me:
Please Sign up or sign in to vote.
3.39/5 (14 votes)
24 May 20041 min read 97.1K   33   19
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:

C#
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


Written By
Web Developer
Cyprus Cyprus
Simpsons' fanatic.

Comments and Discussions

 
AnswerAnother way to resolve this scenario Pin
Kalaimani V19-Nov-09 2:26
Kalaimani V19-Nov-09 2:26 
GeneralA shorter solution (maybe) Pin
C. Augusto Proiete5-Jul-09 14:46
C. Augusto Proiete5-Jul-09 14:46 
GeneralSolution with C++ (even shorter than c#) ; P Pin
Lemontee20-Jan-09 1:35
Lemontee20-Jan-09 1:35 
Generalsingle mdichild form Pin
cristi_io22-May-08 21:14
cristi_io22-May-08 21:14 
GeneralThis is how I did it Pin
denzeo26-Oct-07 16:39
denzeo26-Oct-07 16:39 
QuestionIsn't this shorter? Logically, no need of boolean Pin
zinczinc26-Apr-07 13:16
zinczinc26-Apr-07 13:16 
AnswerRe: Isn't this shorter? Logically, no need of boolean Pin
RAGolko29-Nov-07 1:49
RAGolko29-Nov-07 1:49 
if you have a number of forms you can use this function:

private bool FormIsOpen(string strFormName)
{
Form[] charr = this.MdiChildren;

if (charr.Length == 0) // no child form is opened
{
return false;
}
else // child forms are opened
{

foreach (Form chform in charr)
{
if (chform.Name == strFormName)
// one instance of the form is already opened
{
chform.Activate();
return true;
}
}
}
return false;
}


then call it like this:

if (! FormIsOpen("RelatedProducts"))
{
RelatedProducts frmRelatedProducts = new RelatedProducts();
frmRelatedProducts.MdiParent = this;
frmRelatedProducts.StartPosition = FormStartPosition.Manual;
frmRelatedProducts.Location = new Point(0, 0);
frmRelatedProducts.Show();
}

DotNetNuke modules, Databases, PocketPC interfaces, web sites and web marketing
GeneralShort Code Pin
hkjadav5-Apr-07 8:21
hkjadav5-Apr-07 8:21 
GeneralHi Pin
pannujagwinder15-Sep-06 10:20
pannujagwinder15-Sep-06 10:20 
Generaltry this Pin
shabonaa4-Mar-06 2:57
shabonaa4-Mar-06 2:57 
Questionhow to pass data from chil to parent Pin
Cangrejo_26069-Nov-05 6:26
Cangrejo_26069-Nov-05 6:26 
AnswerRe: how to pass data from chil to parent Pin
Polis Pilavas21-Nov-05 8:13
Polis Pilavas21-Nov-05 8:13 
AnswerRe: Why not disable the "form" button/menu item? Pin
leonleslie5-Dec-04 9:57
leonleslie5-Dec-04 9:57 
GeneralSingleton Pattern Pin
David Veeneman5-Jun-04 14:56
David Veeneman5-Jun-04 14:56 
GeneralI am using next code Pin
akorolev104-Jun-04 21:47
akorolev104-Jun-04 21:47 
GeneralSuggestion... Pin
David M. Kean25-May-04 2:21
David M. Kean25-May-04 2:21 
GeneralPatterns Pin
Christopher Pearce25-May-04 2:07
Christopher Pearce25-May-04 2:07 
QuestionWhy not disable the "form" button/menu item? Pin
Marc Clifton25-May-04 1:43
mvaMarc Clifton25-May-04 1:43 
AnswerRe: Why not disable the "form" button/menu item? Pin
Polis Pilavas27-May-04 21:09
Polis Pilavas27-May-04 21:09 

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

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