Solved

Form open more than 1 time c#

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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