WinForm單例窗體用法實(shí)例
本文實(shí)例講述了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>釋放對(duì)象 /// </summary> /// <param name="obj"></param> /// <param name="args"></param> private static void Display(object obj, FormClosedEventArgs args) { form = null; list.Remove(form); } /// <summary>創(chuàng)建新窗體 /// </summary> private static void CreateNewForm(T t1) { form = t1; form.FormClosed += new FormClosedEventHandler(Display);//訂閱窗體的關(guān)閉事件,釋放對(duì)象 } /// <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; } //如果窗體的文本相同則認(rèn)為是同一個(gè)窗體 foreach (var t in list) { if (t.Text == T1.Text) return true; } list.Add(T1); return false; } } }
調(diào)用如下:
不帶參數(shù)的構(gòu)造函數(shù)
Customer.AddCustomer customer = Common.FormSingle<Customer.AddCustomer>.GetForm(new Customer.AddCustomer()); customer.MdiParent = this;//Mdi窗體 customer.WindowState = FormWindowState.Maximized;//最大化 customer.Show(); customer.Activate();
帶參數(shù)的構(gòu)造函數(shù)
Customer.AddCustomer customer = Common.FormSingle<Customer.AddCustomer>.GetForm(new Customer.AddCustomer(customerid)); customer.MdiParent = this; customer.WindowState = FormWindowState.Maximized; customer.Show(); customer.Activate();
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《WinForm控件用法總結(jié)》、《C#窗體操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
上一篇:C#實(shí)現(xiàn)的字符串轉(zhuǎn)MD5碼函數(shù)實(shí)例
欄 目:C#教程
本文標(biāo)題:WinForm單例窗體用法實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6377.html
您可能感興趣的文章
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并打開(kāi)的方法
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)Winform中打開(kāi)網(wǎng)頁(yè)頁(yè)面的方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法
- 01-10C#實(shí)現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10WinForm實(shí)現(xiàn)自定義右下角提示效果的方法
- 01-10.net2.0+ Winform項(xiàng)目實(shí)現(xiàn)彈出容器層
- 01-10C#實(shí)現(xiàn)主窗體最小化后出現(xiàn)懸浮框及雙擊懸浮框恢復(fù)原窗體的方


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文