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.
Two options, depending on what you need:
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.
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.
do something like:
SingleForm myform = null;
void ShowMyForm_Click(object sender, EventArgs e)
{ if (myform == null)
{
myform = new SingleForm();
}
myform.Show();
myform.BringToFront();
}
Force your form object to adhere to the singleton pattern
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With