Solved

Form open more than 1 time c#

Posted on 2010-11-15
5
775 Views
Last Modified: 2013-12-17
Hello. So i have a problem that my form more than 1 time.

How can i check if the form is open and if it opened i show?
0
Comment
Question by:juliodiz
5 Comments
 
LVL 12

Expert Comment

by:Mamtha1982
Comment Utility
0
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
If you are talking about the application in whole (main form) the following may help:
http://codebetter.com/blogs/paul.laudeman/archive/2004/07/17/Windows-Forms-Tip_3A00_-Ensure-only-one-instance-of-your-application-is-running-at-a-time.aspx

However, if you are talking about one of "secondary" forms, i.e. forms you are opening from your application, you may just check a reference to a form. You have to have an only variable to reference the form. If it is nothing - you create and open a new form. If not - show existing form.
0
 

Author Comment

by:juliodiz
Comment Utility
This?

        public static void OpenForm(Type frmType)
        {
            bool bolCtl = false;
            foreach (Form form in Application.OpenForms)
            {
                if (form.GetType().Equals(frmType))
                {
                    form.Show();
                    bolCtl = true;
                    break;
                }
            }

            if (!bolCtl)
            {
                Form frm = (Form)Activator.CreateInstance(frmType);
                frm.Show();
            }
        }


        public static void LoadForm1()
        {
            OpenForm(typeof(Login));

            //Login _form1 = new Login();
            //_form1.Show();
        }
0
 
LVL 7

Accepted Solution

by:
keustermans earned 500 total points
Comment Utility
There is an easier way of doing this with a lot less code and without having to loop through the forms,
Check below:

I created 3 forms Form1, Form2 and Form3.  I used Form1 as the master form and added 2 buttons
one to open Form2 and one to open Form3.

In Form2 I added a private static variable of Type Form2 and called it form2, I then added a public Static Method that returns an instance of form2 the resr you can check out in the code below:

You will notice that each form wil lonly open once.
//Form1

 public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }



        private void button1_Click(object sender, EventArgs e)

        {

            Form2.GetForm().Show();

        }



        private void button2_Click(object sender, EventArgs e)

        {

            Form3.GetForm().Show();

        }

    }



//Form2



public partial class Form2 : Form

    {

        private static Form2 form2;

        public static Form2 GetForm()

        {

            if (form2 == null)

            {

                form2 = new Form2();

                

            }

            return form2;

        }

         



        private void Form2_FormClosing(object sender, FormClosingEventArgs e)

        {

            form2 = null;

        }





        public Form2()

        {

            InitializeComponent();

        }

    }



//Form3



public partial class Form3 : Form

    {

        private static Form3 form3;

        public static Form3 GetForm()

        {

            if (form3 == null)

            {

                form3 = new Form3();



            }

            return form3;

        }



        private void Form3_FormClosing(object sender, FormClosingEventArgs e)

        {

            form3 = null;

        }



        public Form3()

        {

            InitializeComponent();

        }



        

    }

Open in new window

0
 
LVL 21

Expert Comment

by:Alpesh Patel
Comment Utility
Hi,
Yes above both experts are right.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now