Solved

How can I prevent running multiple instances of a form?

Posted on 2007-11-16
5
624 Views
Last Modified: 2013-12-17
Hi,

I have a button with the following code:

FormPrefs FP = new FormPrefs();
FP.Show();

How can I only allow one instance of FormPrefs to run?

Thanks.
0
Comment
Question by:EEssam
  • 3
5 Comments
 
LVL 5

Expert Comment

by:administradores
Comment Utility
If you don't use new, only one form will be showed.
0
 
LVL 5

Assisted Solution

by:administradores
administradores earned 75 total points
Comment Utility
But singletons is the way to go, sorry.

http://msdn2.microsoft.com/en-us/library/ms998558.aspx
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 400 total points
Comment Utility
is it unique in the same application?
or maybe you want to avoid many application instances.

for first, you can do something like:

public class FormPrefs : Form
{
        private static FormPrefs UniqueForm = null;
       
        private FormPrefs()  // private constructor, don't allow to construct outside
        {
               // some tasks
        }
        public static void CreateOrShow()
        {
                if (UniqueForm == null)
                    UniqueForm = new FormPrefs();
                UniqueForm.Show();
        }
}

so you can use as:
FormPrefs.CraeteOrShow();
0
 
LVL 5

Expert Comment

by:administradores
Comment Utility
Also, if you use it as a mdichild form, you can add this line UniqueForm.BringToFront() after UniqueForm.Show() so the form will go to the top of your mdiontainer (form).
0
 
LVL 13

Assisted Solution

by:SameerJagdale
SameerJagdale earned 25 total points
Comment Utility
just create a static instance of form when your application loads and call show() / hide() methods.

Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

10 Experts available now in Live!

Get 1:1 Help Now