?
Solved

Open Form On-Board Singleton?

Posted on 2011-10-27
4
Medium Priority
?
209 Views
Last Modified: 2012-05-12
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.

 
0
Comment
Question by:pointeman
  • 3
4 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37041387
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37041411
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
 

Author Comment

by:pointeman
ID: 37041536
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37041770
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

850 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