I want to simplify some of my code. Therefore I want to make a function which checks, if a certain form is already open. Right now I have the code below behind every button on my start-form.
private void button_parts_Click(object sender, EventArgs e)
{
FormCollection fc = Application.OpenForms;
foreach (Form frm in fc)
{
if (frm is frm_parts) { return; }
}
frm_Teile newForm = new frm_parts();
newForm.Show();
}
Now I would like to have something like:
private void button_parts_Click(object sender, EventArgs e)
{
StartNewForm(frm_parts);
}
private void StartNewForm(Type myForm)
{
FormCollection fc = Application.OpenForms;
foreach (Form frm in fc)
{
if (frm is myForm) { return; }
}
myForm newForm = new myForm();
newForm.Show();
}
But I cannot pass a type to a function
EDIT: You certainly can, but I didn't know how and where to start.
Is there a(nother) way to achieve what I need?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…