Solved

Form open more than 1 time c#

Posted on 2010-11-15
5
776 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
ID: 34142331
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 34142369
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
ID: 34149767
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
ID: 34153072
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
ID: 34153672
Hi,
Yes above both experts are right.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

22 Experts available now in Live!

Get 1:1 Help Now