Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C# 3.5
Tip/Trick

Restricting Windows Form from opening again if its already open

Rate me:
Please Sign up or sign in to vote.
4.64/5 (4 votes)
7 Jul 2014CPOL2 min read 21.2K   5   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

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

C++
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)


Written By
Software Developer
United Arab Emirates United Arab Emirates
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionthoughts Pin
johannesnestler9-Jul-14 2:55
johannesnestler9-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.