Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Multiple Form Instances

How do I prevent Multiple forms from opening?

I do .show on the form but the user can click the main form and the button again and another instance of form opens.

like image 337
JPJedi Avatar asked Feb 01 '10 00:02

JPJedi


3 Answers

Two options, depending on what you need:

  1. Use ShowDialog instead of Show, which will open a modal window. This is the obvious solution if you don't need your main form to be active while the child form is open.

  2. Or keep track of the window you opened already in the main form and do nothing if it's already open. This will be needed if you want the user to be able to use the main form while the child form is already open, maybe to open other forms.

like image 61
Joey Avatar answered Oct 13 '22 20:10

Joey


do something like:

SingleForm myform = null;

void ShowMyForm_Click(object sender, EventArgs e) 
{     if (myform == null)
       {
             myform = new SingleForm();  
        } 
       myform.Show();
       myform.BringToFront(); 
 }
like image 32
IAbstract Avatar answered Oct 13 '22 19:10

IAbstract


Force your form object to adhere to the singleton pattern

like image 26
Andrew Sledge Avatar answered Oct 13 '22 20:10

Andrew Sledge