文章目录
-
[2 基本设置](#2 基本设置)
-
[3 实例:多窗口重叠、水平平铺、垂直平铺](#3 实例:多窗口重叠、水平平铺、垂直平铺)
-
- [3.1 主窗口属性设置](#3.1 主窗口属性设置)
- [3.2 主窗口](#3.2 主窗口)
- [3.3 主窗口窗口添加MenuStrip菜单](#3.3 主窗口窗口添加MenuStrip菜单)
- [3.4 添加处理函数](#3.4 添加处理函数)
- [3.5 测试效果](#3.5 测试效果)
-
[4 利用窗体参数定义进行传值](#4 利用窗体参数定义进行传值)
-
- [4.1 在Form2、Form3添加相关控件](#4.1 在Form2、Form3添加相关控件)
- [4.2 Form3 定义函数public Form3(string varName, string varEmail)](#4.2 Form3 定义函数public Form3(string varName, string varEmail))
- [4.3 在Form2添加处理函数](#4.3 在Form2添加处理函数)
- [4.4 运行结果](#4.4 运行结果)
-
[5 避免重复打开同一个子窗口](#5 避免重复打开同一个子窗口)
-
[6 通过类属性进行数据传值](#6 通过类属性进行数据传值)
-
WinForm 是 Windows Form 的简称,是基于 .NET Framework 平台的客户端(PC软件)开发技术,是 C# 语言中的一个重要应用。
-
.NET 提供了大量 Windows 风格的控件和事件,可以直接拿来使用。
-
本专栏内容 是按照标题序号逐渐深入的,如有不懂的基础问题,可看前面教程。
-
教程从属性、方法、事件、实例 等方面展开讲解,图文并茂、超详细 ,必要的地方都有 示例、代码 ,最后附 综合实例+源码
1多文档窗体MDI
-
MDI (Multiple Document Interface) 窗体被称为多文档窗体,它是很多 Windows 应用程序中常用的界面设计。
-
将多控件窗体在同一窗体中打开,可以设置重叠打开,平捕打开等,多文档界面,用于同时显示多个文档。
-
在项目中使用MDI窗体时,通常将一个MDI窗口窗体作为父窗体,父窗体可以将多个子窗体包容在它的工作区之中。
2 基本设置
- 1.1 设置:窗口属性 中
IsMDIContainer
设为true
;
当然,也可以在程序设定
cpp
this.IsMdiContainer = True;
- 1.2 子级窗体在MDI中打开,需先设置位于MDI窗体中
cpp
Form1 f3 = new Form1();
f3.MdiParent = this;
f3.Show();
- 1.3 窗口打开最大化
cpp
f3.WindowState=FormwindowState.Maximized
3 实例:多窗口重叠、水平平铺、垂直平铺
排列MDI窗体函数 :public void LayoutMdi(MdiLayout value)
value是MdiLayout的枚举值之一,用来定义MDI子窗体的布局。
参数 | 含义 |
---|---|
Cascade | 层叠排列MDI子窗体 |
TileHorizontal | 水平平铺MDI子窗体 |
TileVertical | 垂直平铺MDI子窗体 |
3.1 主窗口属性设置
1 新建一个主窗口,并将属性IsMdiContainer
设为 True

3.2 主窗口
- 2 .添加窗口 Form2 ( 一会代码中用Form2创建新的窗口)
3.3 主窗口窗口添加MenuStrip菜单
- 3 From1窗口添加MenuStrip菜单
并添加 菜单子选项
3.4 添加处理函数
双击 "子窗口2 "、"重叠窗口 "、"水平平铺 "、"垂直平铺",进入事件函数,添加处理代码
cpp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormTest2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 窗口2ToolStripMenuItem_Click(object sender, EventArgs e)//创建新窗口
{
Form2 Mdichild = new Form2();
Mdichild.MdiParent = this;
Mdichild.Show();
}
private void 重叠窗口ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.Cascade);
}
private void 水平平铺ToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.TileHorizontal);
}
private void 垂直平铺ToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.LayoutMdi(MdiLayout.TileHorizontal);
}
}
}
3.5 测试效果

点击子窗口2
,新建窗口
下图分别是 "重叠窗口 "、"水平平铺 "、"垂直平铺 " 效果
4 利用窗体参数定义进行传值
- 添加Form3 构造函数 定义相关参数
public Form3(string varName, string varEmail)
- 在Form2里创建Form3实例并传入参数,在Form3里接收处理相关参数
4.1 在Form2、Form3添加相关控件
4.2 Form3 定义函数public Form3(string varName, string varEmail)
Form2 .c3
cpp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormTest2
{
public partial class Form3 : Form
{
// 定义私有变量
private string _name;
private string _email;
// Form3 定义相关参数
public Form3(string varName, string varEmail)
{
InitializeComponent();
this._name = varName;
this._email = varEmail;
listBox1.Items.Add(this._name);
listBox1.Items.Add(this._email);
}
private void button1_Click(object sender, EventArgs e)
{
//MessageBox.Show("感谢使用!");
Form2 form2 = new Form2();
form2.MdiParent = this.MdiParent; // 设置Form2受MDI控制
form2.Show();
this.Close();
}
}
}
4.3 在Form2添加处理函数
Form2 .cs
cpp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormTest2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = "";
textBox1.Focus();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("姓名或邮箱不能为空!", "信息提示");
}
else
{
this.Hide();
Form3 childForm3 = new Form3(this.textBox1.Text, this.textBox2.Text);
childForm3.MdiParent = this.MdiParent; // 设置Form3受MDI控制
childForm3.Show();
}
}
}
}
4.4 运行结果

5 避免重复打开同一个子窗口
前面的例子中,我们点击子菜单中的 "子窗口2",会重复创建打开多个 "Form2
"

cpp
private void 窗口2ToolStripMenuItem_Click(object sender, EventArgs e)//创建新窗口
{
// 打开子窗体
Form2 Mdichild = new Form2();
Mdichild.MdiParent = this;
Mdichild.Show();
}
检查是否已经打开了此MDI窗体,就弹窗提示,并结束;避免重复打开同一个子窗口
cpp
private void 窗口2ToolStripMenuItem_Click(object sender, EventArgs e)//创建新窗口
{
// 检查是否已经打开了此MDI窗体,避免重复打开同一个子窗口
foreach (Form childrenForm in this.MdiChildren)
{
// 检查是不是当前子窗体名称
if (childrenForm.Name == "Form2")
{
// 是则显示,并激活该窗体
childrenForm.Visible = true;
childrenForm.Activate();
MessageBox.Show(childrenForm.Name+ "已经打开,请勿重复打开同一个子窗口");
return;
}
}
// 打开子窗体
Form2 Mdichild = new Form2();
Mdichild.MdiParent = this;
Mdichild.Show();
}
打开 Form2
重复打开 Form2时
6 通过类属性进行数据传值
添加 窗口 Form4,并在 Form4中添加 listBox控件
;
在Form4空白处双击,进入Form4_Load(object sender, EventArgs e)
函数;
Form4.cs
cpp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormTest2
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private string _name, email_address, topic, option;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string EmailAddress
{
get
{
return email_address;
}
set
{
email_address = value;
}
}
public string Topic
{
get { return topic; }
set { topic = value; }
}
public string Option
{
get { return option; }
set { option = value; }
}
private void Form4_Load(object sender, EventArgs e)
{
listBox1.Items.Add(_name);
listBox1.Items.Add(email_address);
listBox1.Items.Add(topic);
listBox1.Items.Add(option);
}
}
}
在Form2.cs的 button 中 创建窗口Form4实例并设置参数值
cpp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormTest2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = "";
textBox1.Focus();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("姓名或邮箱不能为空", "信息提示");
}
else
{
this.Hide();
Form4 childForm4 = new Form4();
childForm4.Name = textBox1.Text;
childForm4.EmailAddress = textBox2.Text;
childForm4.Topic = "Topic a";
childForm4.Option = "Option a";
childForm4.MdiParent = this.MdiParent;
childForm4.Show();
}
}
}
}
