• C# Winform编程(4)多文档窗口(MDI)


    创建多文档窗口

    添加菜单,IsMdiContainer设为True:
    在这里插入图片描述
    From窗口添加菜单
    在这里插入图片描述

    Form1.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 多文档界面MDI
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void 视频ToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
            }
    
            private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
            {
    
            }
    
            private void 窗口ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                //MessageBox.Show("窗口被单击", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
    
            private void 子窗口2ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Form2 Mdichild = new Form2();
                Mdichild.MdiParent = this;
                Mdichild.Show();
            }
    
            private void 子窗口3ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                
            }
    
            private void 窗口层叠ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.LayoutMdi(MdiLayout.Cascade);
            }
    
            private void 水平平铺ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.LayoutMdi(MdiLayout.TileHorizontal);
            }
    
            private void 垂直平铺ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.LayoutMdi(MdiLayout.TileVertical);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    在这里插入图片描述

    利用窗体参数定义进行传值

    • 添加Form3 构造函数定义相关参数public Form3(string varName, string varEmail)
    • 在Form2里创建Form3实例并传入参数,在Form3里接收处理相关参数

    Form3.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 多文档界面MDI
    {
        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 Form3_Load(object sender, EventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //MessageBox.Show("感谢使用!");
                Form2 form2 = new Form2();
                form2.MdiParent = this.MdiParent; // 设置Form2受MDI控制
                form2.Show();
                this.Close();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    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 多文档界面MDI
    {
        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();
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
    
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    避免重复打开同一个子窗口

     private void 子窗口2ToolStripMenuItem_Click(object sender, EventArgs e)
     {
         // 检查是否已经打开了此MDI窗体
         foreach (Form childrenForm in this.MdiChildren)
         {
             // 检查是不是当前子窗体名称
             Console.WriteLine("Name:", childrenForm.Name);
             if (childrenForm.Name == "Form2")
             {
                 // 是则显示,并激活该窗体
                 childrenForm.Visible = true;
                 childrenForm.Activate();
                 return;
             }
         }
         // 打开子窗体
         Form2 Mdichild = new Form2();
         Mdichild.MdiParent = this;
         Mdichild.Show();
     }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    通过类属性进行数据传值

    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 多文档界面MDI
    {
        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);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    创建窗口Form4实例并设置参数值

    private void button3_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();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    HTTP代理与SOCKS5代理,有什么区别?
    洛谷P3574 FAR-FarmCraft
    Side Window Filtering 论文笔记
    Vue开发实战02-vue项目添加状态管理Vuex,路由router,以及http请求axios
    数据库方向上的九种职业
    管道读写特点以及设置成非阻塞
    Linux环境变量详解
    数据结构(高阶)—— AVL树
    【机器学习】面试题:LSTM长短期记忆网络的理解?LSTM是怎么解决梯度消失的问题的?还有哪些其它的解决梯度消失或梯度爆炸的方法?
    MyBatis学习笔记(一)
  • 原文地址:https://blog.csdn.net/u013420428/article/details/133865735