Solved

How can I prevent running multiple instances of a form?

Posted on 2007-11-16
5
626 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
ID: 20302898
If you don't use new, only one form will be showed.
0
 
LVL 5

Assisted Solution

by:administradores
administradores earned 75 total points
ID: 20302910
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
ID: 20302933
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
ID: 20302998
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
ID: 20303178
just create a static instance of form when your application loads and call show() / hide() methods.

Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

20 Experts available now in Live!

Get 1:1 Help Now