文章目錄
- 1多文檔窗體MDI
- 2 基本設置
- 3 實例:多窗口重疊、水平平鋪、垂直平鋪
- 3.1 主窗口屬性設置
- 3.2 主窗口
- 3.3 主窗口窗口添加MenuStrip菜單
- 3.4 添加處理函數
- 3.5 測試效果
- 4 利用窗體參數定義進行傳值
- 4.1 在Form2、Form3添加相關控件
- 4.2 Form3 定義函數public Form3(string varName, string varEmail)
- 4.3 在Form2添加處理函數
- 4.4 運行結果
- 5 避免重復打開同一個子窗口
- 6 通過類屬性進行數據傳值
-
WinForm 是 Windows Form 的簡稱,是基于 .NET Framework 平臺的客戶端(PC軟件)開發技術,是 C# 語言中的一個重要應用。
-
.NET 提供了大量 Windows 風格的控件和事件,可以直接拿來使用。
-
本專欄內容是按照標題序號逐漸深入的,如有不懂的基礎問題,可看前面教程。
-
教程從屬性、方法、事件、實例等方面展開講解,圖文并茂、超詳細,必要的地方都有 示例、代碼 ,最后附 綜合實例+源碼
1多文檔窗體MDI
-
MDI (Multiple Document Interface) 窗體被稱為多文檔窗體,它是很多 Windows 應用程序中常用的界面設計。
-
將多控件窗體在同一窗體中打開,可以設置重疊打開,平捕打開等,多文檔界面,用于同時顯示多個文檔。
-
在項目中使用MDI窗體時,通常將一個MDI窗口窗體作為父窗體,父窗體可以將多個子窗體包容在它的工作區之中。
2 基本設置
- 1.1 設置:窗口屬性中
IsMDIContainer
設為true
;
當然,也可以在程序設定
this.IsMdiContainer = True;
- 1.2 子級窗體在MDI中打開,需先設置位于MDI窗體中
Form1 f3 = new Form1();
f3.MdiParent = this;
f3.Show();
- 1.3 窗口打開最大化
f3.WindowState=FormwindowState.Maximized
3 實例:多窗口重疊、水平平鋪、垂直平鋪
排列MDI窗體函數:public void LayoutMdi(MdiLayout value)
value是MdiLayout的枚舉值之一,用來定義MDI子窗體的布局。
參數 | 含義 |
---|---|
Cascade | 層疊排列MDI子窗體 |
TileHorizontal | 水平平鋪MDI子窗體 |
TileVertical | 垂直平鋪MDI子窗體 |
3.1 主窗口屬性設置
1 新建一個主窗口,并將屬性IsMdiContainer
設為True
3.2 主窗口
- 2 .添加窗口 Form2 ( 一會代碼中用Form2創建新的窗口)
3.3 主窗口窗口添加MenuStrip菜單
- 3 From1窗口添加MenuStrip菜單
并添加 菜單子選項
3.4 添加處理函數
雙擊 “子窗口2”、“重疊窗口”、“水平平鋪”、“垂直平鋪”,進入事件函數,添加處理代碼
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 WinFormTest2
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void 窗口2ToolStripMenuItem_Click(object sender, EventArgs e)//創建新窗口{Form2 Mdichild = new Form2();Mdichild.MdiParent = this;Mdichild.Show();}private void 重疊窗口ToolStripMenuItem_Click(object sender, EventArgs e){this.LayoutMdi(MdiLayout.Cascade);}private void 水平平鋪ToolStripMenuItem1_Click(object sender, EventArgs e){this.LayoutMdi(MdiLayout.TileHorizontal);}private void 垂直平鋪ToolStripMenuItem1_Click(object sender, EventArgs e){this.LayoutMdi(MdiLayout.TileHorizontal);}}
}
3.5 測試效果
點擊子窗口2
,新建窗口
下圖分別是 “重疊窗口”、“水平平鋪”、“垂直平鋪” 效果
4 利用窗體參數定義進行傳值
- 添加Form3 構造函數定義相關參數
public Form3(string varName, string varEmail)
- 在Form2里創建Form3實例并傳入參數,在Form3里接收處理相關參數
4.1 在Form2、Form3添加相關控件
4.2 Form3 定義函數public Form3(string varName, string varEmail)
Form2 .c3
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 WinFormTest2
{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 button1_Click(object sender, EventArgs e){//MessageBox.Show("感謝使用!");Form2 form2 = new Form2();form2.MdiParent = this.MdiParent; // 設置Form2受MDI控制form2.Show();this.Close();}}
}
4.3 在Form2添加處理函數
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 WinFormTest2
{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();}}}
}
4.4 運行結果
5 避免重復打開同一個子窗口
前面的例子中,我們點擊子菜單中的 “子窗口2”,會重復創建打開多個“Form2
”
private void 窗口2ToolStripMenuItem_Click(object sender, EventArgs e)//創建新窗口{// 打開子窗體Form2 Mdichild = new Form2();Mdichild.MdiParent = this;Mdichild.Show();}
檢查是否已經打開了此MDI窗體,就彈窗提示,并結束;避免重復打開同一個子窗口
private void 窗口2ToolStripMenuItem_Click(object sender, EventArgs e)//創建新窗口{// 檢查是否已經打開了此MDI窗體,避免重復打開同一個子窗口foreach (Form childrenForm in this.MdiChildren){// 檢查是不是當前子窗體名稱if (childrenForm.Name == "Form2"){// 是則顯示,并激活該窗體childrenForm.Visible = true;childrenForm.Activate();MessageBox.Show(childrenForm.Name+ "已經打開,請勿重復打開同一個子窗口");return;}}// 打開子窗體Form2 Mdichild = new Form2();Mdichild.MdiParent = this;Mdichild.Show();}
打開 Form2
重復打開 Form2時
6 通過類屬性進行數據傳值
添加 窗口 Form4,并在 Form4中添加 listBox控件
;
在Form4空白處雙擊,進入Form4_Load(object sender, EventArgs e)
函數;
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 WinFormTest2
{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); }}
}
在Form2.cs的 button 中 創建窗口Form4實例并設置參數值
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 WinFormTest2
{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();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();}}}
}