Solved

Form open more than 1 time c#

Posted on 2010-11-15
5
788 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 12

Expert Comment

by:Praveen Kumar
ID: 34142331
0
 
LVL 30

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

636 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