Solved

Form open more than 1 time c#

Posted on 2010-11-15
5
779 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

816 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

12 Experts available now in Live!

Get 1:1 Help Now