I have an MDI Application and each child Form needs to be initialized using the same operations:
private void button1_Click(object sender, EventArgs e)
{
frmClient useform=new frmClient();
useform.MdiParent=this;
useform.Text=this.Text+" - "+useform.Text;
useform.Show();
}
I do this for about 5 or 6 different Forms. To avoid repetitions, I have a method to create instances of child Forms:
private void OpenFocus<frmType>()
{
//iterate child forms and if this type is found, set it as focused and return true
if (IsFormOpen(typeof(frmType)))
{
return;
}
var useform = new Form();
switch (typeof(frmType).Name)
{
case "frmCompany":
useform = new frmCompany();
break;
case "frmClients":
useform = new frmClients();
break;
/*
a whole bunch more forms
...
*/
default:
return;
}
useform.MdiParent=this;
useform.Text=this.Text+" - "+useform.Text;
useform.Show();
}
Called like:
private void button1_Click(object sender, EventArgs e)
{
OpenFocus<frmCompany>();
}
private void button2_Click(object sender, EventArgs e)
{
OpenFocus<frmClients>();
}
I have tried
var useform=Activator.CreateInstance<frmType>();
but it is extremely slow, so am avoiding it.
Any suggestions to improve this? It doesn't feel right.
question from:
https://stackoverflow.com/questions/66052616/simplify-the-creation-of-mdi-child-forms-based-on-their-type 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…