zoukankan      html  css  js  c++  java
  • Winform单例窗体

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Text;
    namespace Common
    {
        /// <summary>
        /// 窗体的单例模式
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class FormSingle<T> where T : Form, new()
        {
            private static T form;
            private static IList<T> list { get; set; }
            public static T GetForm(T t1)
            {
                //检查是否存在窗体
                if (!IsExist(t1))
                {
                   CreateNewForm(t1);
                }
                return form;
            }
            /// <summary>释放对象
            /// </summary>
            /// <param name="obj"></param>
            /// <param name="args"></param>
            private static void Display(object obj, FormClosedEventArgs args)
            {
                form = null;
                list.Remove(form);
            }
            /// <summary>创建新窗体
            /// </summary>
            private static void CreateNewForm(T t1)
            {
                form = t1;
                form.FormClosed += new FormClosedEventHandler(Display);//订阅窗体的关闭事件,释放对象
            }
            /// <summary>
            /// 是否存在该窗体
            /// </summary>
            /// <param name="T1"></param>
            /// <returns></returns>
            private static bool IsExist(T T1)
            {
                if (list == null)
                {
                    list=new List<T>();
                    list.Add(T1);
                    return false;
                }
                //如果窗体的文本相同则认为是同一个窗体
                foreach (var t in list)
                {
                    if (t.Text == T1.Text)
                        return true;
                }
                list.Add(T1);
                return false;
            }
        }
    }
    
    调用如下:
      
    不带参数的构造函数  
    Customer.AddCustomer customer = Common.FormSingle<Customer.AddCustomer>.GetForm(new Customer.AddCustomer());
                customer.MdiParent = this;//Mdi窗体
                customer.WindowState = FormWindowState.Maximized;//最大化
                customer.Show();
                customer.Activate();
    带参数的构造函数
      Customer.AddCustomer customer = Common.FormSingle<Customer.AddCustomer>.GetForm(new Customer.AddCustomer(customerid));
                customer.MdiParent = this;
                customer.WindowState = FormWindowState.Maximized;
                customer.Show();
                customer.Activate();


  • 相关阅读:
    jQuery操作CheckBox的方法(选中,取消,取值)详解
    checkAll操作
    java 去掉重复的数字
    multiselect多选下拉框
    toggle() 隐藏和收缩
    Test 6.29 T4 简单数据结构练习
    Test 6.29 T3 小学生
    Test 6.29 T2 染色
    Test 6.29 T1 预算方案
    [洛谷P3338] ZJOI2014 力
  • 原文地址:https://www.cnblogs.com/huangtailang/p/2041033.html
Copyright © 2011-2022 走看看