MDI多文档界面管理

本次目标是建立一个Form主窗体,并在该主窗体中建立菜单,通过菜单打开其余的子窗体。

Form1图片:

 

 

 代码:

 

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 WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 

        private void menuItem3_Click(object sender, EventArgs e)
        {

            {
                //foreach (Form childrenForm in this.MdiChildren)
                //{
                //    if (childrenForm.Name == "Form2")
                //    {
                //        childrenForm.Visible = true;
                //        childrenForm.Activate();
                //        return;
                //    }
                //}

               int i = OnlyOne("Form2");
                if (i == 0)
                {
                    Form2 Mdichild = new Form2();
                    Mdichild.MdiParent = this;
                    Mdichild.Show();
                   // this.LayoutMdi(MdiLayout.TileHorizontal);
                }
                else
                    return;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            int a = OnlyOne("Form2");
            if (a == 0)
           
            {
                Form2 MdiChild1 = new Form2();
                MdiChild1.MdiParent = this;
                MdiChild1.Show();
                //this.LayoutMdi(MdiLayout.TileHorizontal);
            }
            else
                return;
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            int b = OnlyOne("Form4");
            if (b == 0)
            {
                Form4 MdiChild2 = new Form4();
                MdiChild2.MdiParent = this;
                MdiChild2.Show();
                //this.LayoutMdi(MdiLayout.TileHorizontal);
            }
            else
                return;

        }

      public int OnlyOne(string xx)     //不重复打开mdi窗口整合成一个方法
        {
            
            foreach (Form childrenForm in this.MdiChildren)
            {
                if (childrenForm.Name == xx)
                {
                    childrenForm.Visible = true;
                    childrenForm.Activate();//有这个,from必须要有可焦点的,如textbox,去掉则不用
                    return 1;
                }
            }
            return 0;
        }
    }
}
View Code

 

Form2图片:

代码:

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 WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
                MessageBox.Show("禁止为空", "信息提示");
            else
            {
               // this.Hide();
                Form3 childForm3 = new Form3(this.textBox1.Text,this.comboBox1.SelectedItem.ToString(),this.listBox1.Text);
                childForm3.MdiParent = this.MdiParent;
                childForm3.Show();  

            }
        }
    }
}
View Code

Form3图片:

代码:

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 WindowsFormsApp1
{
    public partial class Form3 : Form
    {
        private string _varname;
        private string _varSubject;
        private string _varFeedBack;
        public Form3(string varName, string varSubject, string varFeedBack)
        {
            InitializeComponent();
            this._varname = varName;
            this._varSubject = varSubject;
            this._varFeedBack = varFeedBack;
            listBox1.Items.Add("姓名:" + _varname);
            listBox1.Items.Add("主题:" + _varSubject);
            listBox1.Items.Add("意见:" + _varFeedBack);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
View Code

Form4图片:

代码:

无特殊功能代码。

 

 

 窗口1的单击事件,得到Form2:

   Form2 Mdichild = new Form2();
   Mdichild.MdiParent = this;    //制定即将打开的Form2对象MdiParent,即Form2对象的MDI父窗口为当前主窗口
   Mdichild.Show();

 

 MDI窗体的排列:

(1)层叠

this.LayoutMdi(MdiLayout.Cascade);

(2)水平平铺

this.LayoutMdi(MdiLayout.TileHorizontal);

(3)垂直平铺

this.LayoutMdi(MdiLayout.TileVertical);

 

 

窗体传值,接受来自Form2的数据信息。

    private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
                MessageBox.Show("禁止为空", "信息提示");
            else
            {
               // this.Hide();
                Form3 childForm3 = new Form3(this.textBox1.Text,this.comboBox1.SelectedItem.ToString(),this.listBox1.Text);
                childForm3.MdiParent = this.MdiParent;
                childForm3.Show();  

            }
        }
View Code

 

确保子窗体在转换的过程中受到MDI主窗口的控制

 childForm3.MdiParent = this.MdiParent;

防止重复打开窗口

        private void menuItem3_Click(object sender, EventArgs e)
        {
                 foreach (Form childrenForm in this.MdiChildren)
                {
                    if (childrenForm.Name == "Form2")
                    {
                        childrenForm.Visible = true;
                        childrenForm.Activate();
                        return;
                    }
                }
               Form2 Mdichild = new Form2();
               Mdichild.MdiParent = this;
               Mdichild.Show();

         }
    
View Code

由于每个打开的窗体都可能会用到上面的代码,因此建议将之集成方法后统一调用。

但是要注意return在防止打开窗口中起到的作用,应用到方法中的函数后,return跳不出事件函数,所以采取了如下方法:

 public int OnlyOne(string xx)     //不重复打开mdi窗口整合成一个方法
        {
            
            foreach (Form childrenForm in this.MdiChildren)
            {
                if (childrenForm.Name == xx)
                {
                    childrenForm.Visible = true;
                    childrenForm.Activate();//有这个,from必须要有可焦点的,如textbox,去掉则不用
                    return 1;
                }
            }
            return 0;
        }

如若没有文本框激活焦点,则不用写childrenFrom.Activate();

                                                                                                                                     


2017-12-04  16:56:54

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值