Open Form On-Board Singleton?

I have code with detects if a form2 is already open from Form1. Just wondering if there is a Singleton code that actually resides on Form2 so it detects if itself is already open, if so cancel or close the newer instance.

 
pointemanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
I believe you could do something such as this:

using System.Windows.Forms;

namespace _27419393
{
    public partial class Form2 : Form
    {
        // The single instance of Form2
        private static Form2 _instance;

        /// <summary>
        /// Initializes the <see cref="Form2"/> class.
        /// Static constructors are guaranteed to run
        /// only once during the life of the application.
        /// </summary>
        static Form2()
        {
            _instance = new Form2();
        }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static Form2 Instance
        {
            get { return _instance; }
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="Form2"/> class from being created.
        /// </summary>
        private Form2()
        {
            InitializeComponent();
        }
    }
}

Open in new window


...and if Form2 were your main form, then you might want to change Program.cs to:

using System;
using System.Windows.Forms;

namespace _27419393
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(Form2.Instance);
        }
    }
}

Open in new window

0
käµfm³d 👽Commented:
P.S.

In case it's not obvious, you can no longer do new Form2() outside of the class. Any reference to a "new" Form2 would be made through the Instance property, and that would always return the one single instance that is allowed to be created.
0
pointemanAuthor Commented:
Still not working, I'm probably missing something, here's my code:

1. Form2 is already opened from Form1.
2. Form1 calls form2.Show()
3. Must not use ShowDialog because I cannot click on Form1 for other reasons

[Form2.cs]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Threading;

namespace FormTesting
{
     public partial class Form2 : Form
    {
        private static Form2 _instance;

        static Form2()
        {
            _instance = new Form2();
        }

        public static Form2 Instance
        {
            get { return _instance; }
        }

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load()
        {
        }
}





0
käµfm³d 👽Commented:
I'm probably missing something, here's my code:
Your non-static constructor should be marked private. See line 31 in the top snippet of my example above.

1. Form2 is already opened from Form1.
2. Form1 calls form2.Show()
3. Must not use ShowDialog because I cannot click on Form1 for other reasons
Here's a crude example of what Form1 might look like if I were just starting out writing it:

using System;
using System.Windows.Forms;

namespace _27419393
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2.Instance.Show();
        }
    }
}

Open in new window


Notice that all interaction with Form2 is done through the Instance property (granted it's only one operation in this example). The Instance property is marked static, so all you have to do to use it is make sure you preface it with the class name (Form2).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.