Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Restricting Windows Form from opening again if its already open

0.00/5 (No votes)
7 Jul 2014 1  
Restricting a Child Form Opening Multiple Times in Windows Form

Introduction

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

Background

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.BringToFront();

            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;
                cmpnymasterform.Show();
            }
        }

 

Forget to say ..I had used Winforms with C#.net ..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()

History

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

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