WinForm單例窗體用法實例。本站提示廣大學習愛好者:(WinForm單例窗體用法實例)文章只能為提供參考,不一定能成為您想要的結果。以下是WinForm單例窗體用法實例正文
本文實例講述了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();
願望本文所述對年夜家C#法式設計有所贊助。