All Things Techie With Huge, Unstructured, Intuitive Leaps

C# - Determine if a Form is Open


I had a requirement to determine if a form was open in Visual Studio using C#.  If it wasn't open, I wanted to open a new one, and if it was, I wanted to pop it to the front.  Obviously, I needed a method to see if there was one.

Here is the code for the method:

 private Boolean checkFormOpen(string formName)
        {
            Boolean openForm = false;
            FormCollection fc = Application.OpenForms;
            foreach (Form namer in fc)
            {
                if (namer.Name.Equals(formName))
                {
                   openForm = true;
                }
            }

            return openForm;

        }

I just call this method and pass the handle of the Form name to it.  Hope this helps.

No comments:

Post a Comment