Click here to Skip to main content
13,191,638 members (64,097 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 7 Jul 2014

Restricting Windows Form from opening again if its already open

, 7 Jul 2014
Rate this:
Please Sign up or sign in to vote.
Restricting a Child Form Opening Multiple Times in Windows Form


Its a normal question from the Winform Developers of the intial stage "How can I restrict my child form from opening twice" or "How can I  make a form not to open if its already opened" lets see how it works


I  had a WindowsForm Application with more than 100 Forms and More than 50 MenustripItems. The user opens the form using the click event of the MenustripButtons. Now the issue is the user may open a form for eg FORM1  and  a later they may leave it opened or minimized and had  accidently or knowingly pressed the Same MenustripButton again .since we are using the Form.Show() function to  display a form ,one more instance of form will be created and displayed.So I decided to stop it by restricting the user opening a form again when its already open .

Using the code

I first created a function IsAlreadyOpen() which get the WindowsForm as  input parameter

The Below function will  loop through the all opened formns in the application and check whether the input parameter form is same ass that of any opened form and will return true if the form is already opened and  will return false if the form is not opened

  private bool IsAlreadyOpen(Type formType)


    bool isOpen = false;


    foreach (Form f in Application.OpenForms)


        if (f.GetType() == formType)



            f.WindowState = FormWindowState.Normal;

            isOpen = true;



    return isOpen;


And in the click event of the ToolstripMenuItem I created an instance of required form and  then I send the form to the function IsAlreadyOpen() and if it return false I intialized the instance and showed it

private void companyToolStripMenuItem1_Click(object sender, EventArgs e)
          Master.CompanyMasterForm cmpnymasterform = null;
          bool isFormOpen = IsAlreadyOpen(typeof(Master.CompanyMasterForm));
          if (isFormOpen == false)
              cmpnymasterform = new Master.CompanyMasterForm();
              cmpnymasterform.StartPosition = FormStartPosition.CenterScreen;

              cmpnymasterform.MdiParent = this;


Forget to say ..I had used Winforms with ..and as always Winform Rockzz

Points of Interest

It should have been more simpler if I had created a function for this click event functions also  and pass only formname to that function .which itself call IsAlreadyOpen()


Keep a running update of any changes or improvements you've made here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
United Arab Emirates United Arab Emirates
No Biography provided

You may also be interested in...

Comments and Discussions

Questionthoughts Pin
johannesnestler9-Jul-14 2:55
memberjohannesnestler9-Jul-14 2:55 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171017.2 | Last Updated 7 Jul 2014
Article Copyright 2014 by SREENATH GANGA
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid