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)容感興趣的讀者可查看本站專(zhuān)題:《WinForm控件用法總結(jié)》、《C#窗體操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《C#程序設(shè)計(jì)之線(xiàn)程使用技巧總結(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ì)有所幫助。
- WinForm遍歷窗體所有子控件的方法
- C#,winform,ShowDialog,子窗體向父窗體傳值
- WinForm窗體間傳值的方法
- 用 C# Winform做出全透明的磨砂玻璃窗體效果代碼
- c# winform 關(guān)閉窗體時(shí)同時(shí)結(jié)束線(xiàn)程實(shí)現(xiàn)思路
- WinForm子窗體訪(fǎng)問(wèn)父窗體控件的實(shí)現(xiàn)方法
- winform使用委托和事件來(lái)完成兩個(gè)窗體之間通信的實(shí)例
- C# Winform實(shí)現(xiàn)捕獲窗體最小化、最大化、關(guān)閉按鈕事件的方法
- C# WINFORM 強(qiáng)制讓窗體獲得焦點(diǎn)的方法代碼
- 在類(lèi)庫(kù)或winform項(xiàng)目中打開(kāi)另一個(gè)winform項(xiàng)目窗體的方法
- WinForm實(shí)現(xiàn)窗體最大化并遮蓋任務(wù)欄的方法
相關(guān)文章
C#實(shí)現(xiàn)Winform版計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)Winform版計(jì)算器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
C# TreeView從數(shù)據(jù)庫(kù)綁定數(shù)據(jù)的示例
這篇文章主要介紹了C# TreeView從數(shù)據(jù)庫(kù)綁定數(shù)據(jù)的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C# 實(shí)現(xiàn)與現(xiàn)有.NET事件橋接簡(jiǎn)單實(shí)例
這篇文章主要介紹了C# 實(shí)現(xiàn)與現(xiàn)有.NET事件橋接簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03

