C#设置MDI子窗体只能弹出一个的方法
作者:shichen2014 时间:2022-03-19 23:40:11
Windows程序设计中的MDI(Multiple Document Interface)官方解释就是所谓的多文档界面,与此对应就有单文档界面 (SDI), 它是微软公司从Windows 2.0下的Microsoft Excel电子表格程序开始引入的,Excel电子表格用户有时需要同时操作多份表格,MDI正好为这种操作多表格提供了很大的方便,于是就产生了MDI程序。
新建一个WindowForm程序。得到一个窗体作为我们父窗体Parent。拖入一个menustrip空间。在新建一个窗体FrmChildren作为我们子窗体,界面如下图所示:
其代码如下所示:
public Form1()
{
InitializeComponent();
//将Form1设置为MDI窗体,当然在Form1的IsMdiContainer属性页可以设置
this.IsMdiContainer = true;
}
在menustrip打开子窗体的事件代码如下:
private void tsmiOpenWindow_Click(object sender, EventArgs e)
{
FrmChildren child = FrmChildren.GetWindow();//调用方法
child.MdiParent = this;//设置child的父窗体为当前窗体
child.Show();
}
GetWindow()这个方法在哪里呢。当然是在FrmChildren子窗体里面写
public partial class FrmChildren : Form
{
private FrmChildren() //由 public FrmChildren改为 private FrmChildren
{
InitializeComponent();
}
static FrmChildren fc = null; 创建一个静态对象
public static FrmChildren GetWindow()
{ //当子窗体没有开启或者已经释放。就可以创建新窗体
if (fc==null||fc.IsDisposed)
{
fc = new FrmChildren();
}
return fc;
}
}
第二种方法:
这种方法个人觉得很简单。直接在在menustrip打开子窗体的事件下面写就ok了
private void tsmiOpenWindow_Click(object sender, EventArgs e)
{
#region 方法二Application收集打开的窗体,用索引器来寻找,就是窗体的Name属性
//方法二.如果没有Name为FrmChildren的子船体,实例化创建。和之前的正规做法没有什么差别,只是多了判断。
if (Application.OpenForms["FrmChildren"] == null)
{
FrmChildren child = new FrmChildren();
child.MdiParent = this;
child.Show();
}
else//有Name为FrmChildren的子船体,就直接show()
{
Application.OpenForms["FrmChildren"].Show();
}
#endregion
}
感兴趣的朋友可以调试运行一下本文所述示例,相信会有不小的收获。
标签:C#,MDI,窗体
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#程序打成 一键安装包-InstallShield教程
2022-11-19 06:02:07
![](https://img.aspxhome.com/file/2023/6/88406_0s.jpg)
python调用java的jar包方法
2023-06-23 21:54:24
C#中Linq的去重方式Distinct详解
2022-01-23 03:53:36
![](https://img.aspxhome.com/file/2023/0/86970_0s.png)
Java数据结构之顺序表和链表精解
2021-07-01 14:30:39
![](https://img.aspxhome.com/file/2023/0/88190_0s.png)
C#使用ScrapySharp快速从网页采集数据
2021-08-14 09:25:14
![](https://img.aspxhome.com/file/2023/8/71728_0s.png)
C#修改及重置电脑密码DirectoryEntry实现方法
2021-12-03 05:08:16
基于私钥加密公钥解密的RSA算法C#实现方法
2022-12-01 07:52:37
Java获取json数组对象的实例讲解
2023-08-24 14:55:28
详解Android的两种事件处理机制
2023-10-05 23:37:20
![](https://img.aspxhome.com/file/2023/4/88114_0s.jpg)
使用JPA自定义VO类型转换(EntityUtils工具类)
2023-08-26 14:56:17
C# 实现dataGridView选中一行右键出现菜单的示例代码
2022-05-12 05:15:55
![](https://img.aspxhome.com/file/2023/9/94539_0s.png)
Android 控制wifi 相关操作实例
2023-09-08 20:35:49
浅析c# 线程同步
2022-09-19 18:43:03
Spring Boot启动端口修改方法
2022-11-21 19:37:53
Java实现克隆的三种方式实例总结
2021-11-21 15:26:14
![](https://img.aspxhome.com/file/2023/2/76632_0s.png)
maven依赖版本没有按照最短路径原则生效的解决方案
2023-04-04 20:49:09
Spring Security权限管理实现接口动态权限控制
2022-07-03 12:25:53
![](https://img.aspxhome.com/file/2023/5/83445_0s.png)
Spring boot2X负载均衡和反向代理实现过程解析
2023-02-06 04:18:53
![](https://img.aspxhome.com/file/2023/2/60612_0s.png)
mybatis中批量插入的两种方式(高效插入)
2023-09-16 22:21:07
Java实现雪花算法(snowflake)
2022-10-27 03:43:23
![](https://img.aspxhome.com/file/2023/0/81300_0s.png)