Something like that?:
public void formOpener<T>() where T : Form, new()
{
var openedForm = Application.OpenForms.OfType<T>().FirstOrDefault();
if (openedForm != null)
{
openedForm.Activate();
return;
}
T newForm = new T();
newForm.MdiParent = this;
newForm.Show();
}
For extension methodOfType<T> required using System.Linq;
Using
formOpener<Form1>();
This will show the form, if any. Otherwise will create a new one.
If you can open several forms of type T, use the property Nameto distinguish them.
public void formOpener<T>(string formName) where T : Form, new()
{
var openedForm = Application.OpenForms.OfType<T>()
.Where(x => x.Name == formName).FirstOrDefault();
}
source
share