MDI多文檔界面管理
阿新 • • 發佈:2017-12-04
技術 cli lis color 文本 bob 方法 pla args
本次目標是建立一個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 {View Codepublic 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(); } } } }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
MDI多文檔界面管理