当前位置: 首页 > news >正文

【28】C# WinForm入门到精通 ——多文档窗体MDI【属性、方法、实例、源码】【多窗口重叠、水平平铺、垂直平铺、窗体传值】

文章目录

  • 1多文档窗体MDI
  • 2 基本设置
  • 3 实例:多窗口重叠、水平平铺、垂直平铺
    • 3.1 主窗口属性设置
    • 3.2 主窗口
    • 3.3 主窗口窗口添加MenuStrip菜单
    • 3.4 添加处理函数
    • 3.5 测试效果
  • 4 利用窗体参数定义进行传值
    • 4.1 在Form2、Form3添加相关控件
    • 4.2 Form3 定义函数public Form3(string varName, string varEmail)
    • 4.3 在Form2添加处理函数
    • 4.4 运行结果
  • 5 避免重复打开同一个子窗口
  • 6 通过类属性进行数据传值

  • WinForm 是 Windows Form 的简称,是基于 .NET Framework 平台的客户端(PC软件)开发技术,是 C# 语言中的一个重要应用。

  • .NET 提供了大量 Windows 风格的控件和事件,可以直接拿来使用。

  • 本专栏内容按照标题序号逐渐深入的,如有不懂的基础问题,可看前面教程

  • 教程从属性、方法、事件、实例等方面展开讲解,图文并茂、超详细必要的地方都有 示例、代码 ,最后附 综合实例+源码

1多文档窗体MDI

  • MDI (Multiple Document Interface) 窗体被称为多文档窗体,它是很多 Windows 应用程序中常用的界面设计。

  • 将多控件窗体在同一窗体中打开,可以设置重叠打开,平捕打开等,多文档界面,用于同时显示多个文档。

  • 在项目中使用MDI窗体时,通常将一个MDI窗口窗体作为父窗体,父窗体可以将多个子窗体包容在它的工作区之中。

2 基本设置

  • 1.1 设置:窗口属性IsMDIContainer 设为 true;
    在这里插入图片描述

当然,也可以在程序设定

this.IsMdiContainer = True;
  • 1.2 子级窗体在MDI中打开,需先设置位于MDI窗体中
Form1 f3 = new Form1();
f3.MdiParent = this;        
f3.Show();
  • 1.3 窗口打开最大化
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”、“重叠窗口”、“水平平铺”、“垂直平铺”,进入事件函数,添加处理代码

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

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

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

在这里插入图片描述

        private void 窗口2ToolStripMenuItem_Click(object sender, EventArgs e)//创建新窗口{// 打开子窗体Form2 Mdichild = new Form2();Mdichild.MdiParent = this;Mdichild.Show();}

检查是否已经打开了此MDI窗体,就弹窗提示,并结束;避免重复打开同一个子窗口

        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

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实例并设置参数值

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();}}}
}

在这里插入图片描述

http://www.dtcms.com/a/427233.html

相关文章:

  • 贡井区建设局网站淘宝客做自己的网站
  • 蓝牙发展史
  • 对LED点灯实验的C与汇编的深入分析,提及到volatile
  • 网站建设外包广州网站建设说说外链的建设
  • LevOJ P2080 炼金铺 II [矩阵解法]
  • wordpress网站映射wordpress免费网站国外
  • 哈尔滨企业建站系统西宁建设局官方网站
  • py_innodb_page_info.py表空间分析
  • 有什么做宝宝辅食的网站吗莱阳网站开发
  • tasklet
  • 页面 HTTPS 化实战,从证书部署到真机验证的全流程(证书链、重定向、混合内容、抓包排查)
  • 北京哪家公司做网站电脑上买wordpress
  • 家用机能否做网站服务器泰安房价走势图
  • MC33PT2000控制详解七:软件代码设计1-图形化设置
  • 在租用香港服务器之前如何判断质量
  • 【高清视频】CXL 2.0 over Fibre演示和答疑 - 将内存拉到服务器10米之外
  • 【STM32项目开源】基于STM32的独居老人监护系统
  • 海外服务器怎么测试再不同地方的访问速度?
  • Linux最忙CPU组查找函数和最忙运行队列查找函数
  • 查看未知LiDAR设备的IP地址
  • iOS 0Day漏洞CVE-2025-24085相关PoC利用细节已公开
  • 网站文案框架网络推广策划书
  • 基于Chrome140的FB账号自动化——运行脚本(三)
  • 广州做网站设计镇江建筑公司排名最新
  • RHCA - CL260 | Day12:集群性能建议
  • CNN基础学习(自用)
  • Spring Boot 集成 Kafka 详解
  • MQTT数据集成
  • 网站的会员系统怎么做电商小程序价格
  • Redis 进阶:跳出缓存局限!7 大核心场景的原理与工程化实践