栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

WinForm单例窗体用法实例

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

WinForm单例窗体用法实例

本文实例讲述了WinForm单例窗体。分享给大家供大家参考,具体如下:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
namespace Common
{
  /// 
  /// 窗体的单例模式
  /// 
  /// 
  public class FormSingle where T : Form, new()
  {
    private static T form;
    private static IList list { get; set; }
    public static T GetForm(T t1)
    {
      //检查是否存在窗体
      if (!IsExist(t1))
      {
 CreateNewForm(t1);
      }
      return form;
    }
    /// 释放对象
    /// 
    /// 
    /// 
    private static void Display(object obj, FormClosedEventArgs args)
    {
      form = null;
      list.Remove(form);
    }
    /// 创建新窗体
    /// 
    private static void CreateNewForm(T t1)
    {
      form = t1;
      form.FormClosed += new FormClosedEventHandler(Display);//订阅窗体的关闭事件,释放对象
    }
    /// 
    /// 是否存在该窗体
    /// 
    /// 
    /// 
    private static bool IsExist(T T1)
    {
      if (list == null)
      {
 list=new List();
 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.GetForm(new Customer.AddCustomer());
customer.MdiParent = this;//Mdi窗体
customer.WindowState = FormWindowState.Maximized;//最大化
customer.Show();
customer.Activate();

带参数的构造函数

Customer.AddCustomer customer = Common.FormSingle.GetForm(new Customer.AddCustomer(customerid));
customer.MdiParent = this;
customer.WindowState = FormWindowState.Maximized;
customer.Show();
customer.Activate();

更多关于C#相关内容感兴趣的读者可查看本站专题:《WinForm控件用法总结》、《C#窗体操作技巧汇总》、《C#常见控件用法教程》、《C#程序设计之线程使用技巧总结》、《C#操作Excel技巧总结》、《C#中XML文件操作技巧汇总》、《C#数据结构与算法教程》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》

希望本文所述对大家C#程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/124099.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号