本次目标是建立一个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; } } }
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(); } } } }
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(); } } }
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(); } }
确保子窗体在转换的过程中受到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(); }
由于每个打开的窗体都可能会用到上面的代码,因此建议将之集成方法后统一调用。
但是要注意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