Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7908247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:51:01+00:00 2026-06-03T11:51:01+00:00

I am trying to create a reusable function that can open a single instance

  • 0

I am trying to create a reusable function that can open a single instance of form. Means if a form is not already open it should create and show the new form and if already open it should bring the existing form to front.

I was using the following function,

if (Application.OpenForms["FPSStorageDemo"] == null)
            {
                FPSStorageDemo fp = new FPSStorageDemo();
                fp.Name = "FPSStorageDemo";
                fp.Show();
            }
            else
            {
                ((FPSStorageDemo)Application.OpenForms["FPSStorageDemo"]).BringToFront();
            }

But I have to write this code again and again whereever I have to open a form. But I need a single reusable function that can do this job.

I wrote a function like,

void OpenSingleInstanceForm(Type TypeOfControlToOpen)
        {
            bool IsFormOpen = false;
            foreach (Form fm in Application.OpenForms)
            {
                if (fm.GetType() == TypeOfControlToOpen)
                {
                    IsFormOpen = true;
                    fm.BringToFront();
                    break;
                }
            }

            if (!IsFormOpen)
            {
                Object obj = Activator.CreateInstance(TypeOfControlToOpen);
                //obj.Show(); //Here is the problem
            }
        }

But at the end I don’t know how to show the new form instance. Can anybody suggest how to do it? Is this wrong or there is another way to do this?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:51:03+00:00Added an answer on June 3, 2026 at 11:51 am
    public static class FormUtility
    {
    public static FormType GetInstance<FormType>() where FormType : Form, new()
    {
      FormType output = Application.OpenForms.OfType<FormType>().FirstOrDefault();
      if(output == null)
      {
        output = new FormType();
      }
      //you could add the show/bring to front here if you wanted to, or have the more general method
      //that just gives a form that you can do whatever you want with (or have one of each).
      return output;
    }
    }
    
    //elsewhere
    FormUtility.GetInstance<Form1>.BringToFront();
    

    I’d also like to take the time to mention that while having methods like this are quick and easy to use, in most cases they are not good design. It leads you to the practice of just accessing forms globally rather than ensuring that when forms need to communicate with each other they do so by exposing the appropriate information through the appropriate scope. It makes programs easier to maintain, understand, extend, increases reusability, etc. If you have trouble determining how best for two or more forms to communicate without resorting to public static references to your forms (which is exactly what Application.OpenForms is) then you should feel free to post that question here for us to help you solve.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a view that only houses reusable HTML blocks that can
I'm trying to create a reusable UserControl in WPF that has a Label and
I am trying to create a reusable Cache Manager component which can be plugged
I'm trying to create a generic, reusable view, that looks like a lined notepad.
I am trying create a layout. There are several inner layouts that should like
What I am trying to do is create a bit of reusable code that
I am trying to create some reusable components in Silverlight2 . The difficulty comes
I am trying to create a Validation in a reusable fashion. Purpose: Make the
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Trying to create my first iPhone app that would play back audio. When I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.