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

Tagged as

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

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SREENATH GANGA
Software Developer
United Arab Emirates United Arab Emirates
No Biography provided

Comments and Discussions

 
Questionthoughts Pinmemberjohannesnestler9-Jul-14 2:55 

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 | Mobile
Web03 | 2.8.140827.1 | Last Updated 7 Jul 2014
Article Copyright 2014 by SREENATH GANGA
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid