方法1:泛型
使用方法: frm材料类别维护 f = frmCreate<frm材料类别维护>.createFrom(this);
public class frmCreate<T> where T : Form, new() { private static T instance; public static T createFrom(Form mdi) { if (instance == null || instance.IsDisposed) { instance = new T(); instance.StartPosition = FormStartPosition.CenterScreen; instance.MdiParent = mdi; instance.Show(); } instance.WindowState = FormWindowState.Normal; return instance; } }
方法2:反射
使用方法: Form f = frmCreateByName.createForm(this, "基础数据.frm材料类别维护");
public class frmCreateByName { public static Form createForm(Form mdi, string frmName) { string className = frmName.Substring(frmName.IndexOf(".") + 1); //保证单例问题 foreach (Form frm in Application.OpenForms) { if (frm.Name.Equals(className)) return frm; } Assembly assembly = Assembly.GetExecutingAssembly(); Form instance = assembly.CreateInstance("珠宝加工管理系统." + frmName) as Form; instance.StartPosition = FormStartPosition.CenterScreen; instance.MdiParent = mdi; instance.WindowState = FormWindowState.Normal; instance.Show(); return instance; } }