添加菜单,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);
}
}
}
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();
}
}
}
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)
{
}
}
}
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();
}
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);
}
}
}
创建窗口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();
}
}