Solved

How can I prevent running multiple instances of a form?

Posted on 2007-11-16
5
625 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

867 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

21 Experts available now in Live!

Get 1:1 Help Now