码迷,mamicode.com
首页 > Windows程序 > 详细

窗体单例问题

时间:2015-03-11 19:10:14      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

方法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;
        }
    }

 

窗体单例问题

标签:

原文地址:http://www.cnblogs.com/feihusurfer/p/4330443.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!