• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 637
  • Last Modified:

How can I prevent running multiple instances of a form?

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
EEssam
Asked:
EEssam
  • 3
3 Solutions
 
administradoresCommented:
If you don't use new, only one form will be showed.
0
 
administradoresCommented:
But singletons is the way to go, sorry.

http://msdn2.microsoft.com/en-us/library/ms998558.aspx
0
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
administradoresCommented:
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
 
SameerJagdaleCommented:
just create a static instance of form when your application loads and call show() / hide() methods.

Thanks
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now