Recently, when I was making a software, because of the use of multiple document forms, the menu in Mdicontrainer is mainly used to display the subform, the number of these menus, the menu programming I want to use generic programming, the source code is as follows (a menu)://T as a template parameter, here want to pass a class name, but ....
private void Showmdichild < T > (string mdichildname) where T:icomparable < T >
... {
T FM;
BOOL Hasquery=false;
foreach (Form frm in this. Mdichildren)
if (frm. Name = = mdichildname)
... {
frm. Activate ();
Hasquery = true;
}
if (! Hasquery)
... {
FM = new T (); Can't do this new, hint: The variable type "T" has no new () constraint and cannot create an instance of the type
Fm. MdiParent = this; Nor, hint: "T" does not contain the definition of "mdiparent"
Fm. Show (); Not yet, ditto.
}
}
MenuItem is a menu, Frmaddproblem is a form class and is, of course, inherited by the form class.
The Click event for this menu is used to open an Frmaddproblem form, and if an instance is already open, the
Will be turned on before activation, if not then new one
private void Menuitem_click (object sender, EventArgs e)
... {
Showmdichild<frmaddproblem> ("Frmaddproblem");
}
Error, see the code comments, do not know how to implement in the template function can use template class parameters to new instances of a class. Hurry