MDI(Multiple Document Interface)应用程序,即多文档界面程序是允许创建在单个容器中包含多个窗体的应用程序。多文档揭秘那可同时打开多个文档,简化了文档之间的信息交换。
MDI应用程序是由一个父窗体和多个子窗体组成。
首先:创建父窗体 设置IsMdiContainer属性为true.
其次:创建子窗体
代码:父窗体按钮事件
private void button1_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); //ShowForm(f2); f2.MdiParent = this; f2.WindowState = FormWindowState.Maximized; //f2.Text = "新建文档" + intNumber++; f2.Show(); }
1.创建子窗体方法
private void ShowForm(Form frm) { foreach (Form f in this.MdiChildren) { if (f.Name == frm.Name) { f.Activate(); return; } } frm.MdiParent = this; frm.WindowState = FormWindowState.Normal; frm.Show(); }
2.设置子窗体属性
ChildForm frm = new ChildForm();
frm.MdiParent = this;
frm.FormBorderStyle = FormBorderStyle.None;
frm.MinimizeBox = false;
frm.MinimizeBox = false;
frm.ShowIcon = false;
frm.show();
frm.Dock = DockStyle.Fill;//完全填充到应用程序
3.某个子窗体只打开一次
private bool isExist(string ChildTypeName) { bool b_result = false; foreach (Form frm in MdiChildren) { if (frm.GetType().Name == ChildTypeName) { frm.Activate(); b_result = true; break; } } return b_result; } private void button1_Click(object sender, EventArgs e) { // Form2 f2 = new Form2(); // //ShowForm(f2); // f2.MdiParent = this; // f2.WindowState = FormWindowState.Maximized; // f2.Text = "新建文档" + intNumber++; // f2.Show(); if (!isExist("Form2")) { Form2 frm2 = new Form2(); frm2.MdiParent = this; frm2.Show(); } }
4.排列子窗体
public void LayoutMdi (MdiLayout value)
5.关闭某个开着的子窗体
private void CloseChildForm(string frmName) { for (int i = 0; i < this.MdiChildren.Length; i++) { if (this.MdiChildren[i].Text == frmName) this.MdiChildren[i].Close(); } }
6.父窗体的TextBox接受子窗体TextBox输入
子窗体textBox事件,首先设置父窗体TextBox作用域修改为public
private void textBox1_TextChanged(object sender, EventArgs e) { TextBox text = ((Form1)this.MdiParent).textBox2; text.Text = textBox1.Text; }
7.取出子窗体最大化时显示在父窗体MenuStrip菜单控件上的最大化 还原 空白等按钮
父窗体MenuStrip菜单的ItemAdded事件
private void menuStrip1_ItemAdded(object sender, ToolStripItemEventArgs e) { if (e.Item.Text.Length == 0 || e.Item.Text == "还原(&R)" || e.Item.Text == "最小化(&N)") { e.Item.Visible = false; } }
8.MenuStrip关联类
ToolStripMenuItem 表示在MenuStrip或ContextMenuStrip上显示的可选择选项
ContextMenuStrip 表示快捷菜单
ToolStripDropDownItem 表示为派生自ToolStripItem的控件提供基本功能,当单击某个控件时显示下拉项
9.MenuStrip菜单自动展开
private void 文件ToolStripMenuItem_MouseEnter(object sender, EventArgs e) { 文件ToolStripMenuItem.ShowDropDown(); } private void 文件ToolStripMenuItem_MouseLeave(object sender, EventArgs e) { 文件ToolStripMenuItem.HideDropDown(); }
10.菜单根据子窗体进行变化
通过设置AllowMerge MergeAction和MergeIndex属性。可以讲MDI子菜单追加到MDI父菜单,并且当关闭MDI子窗体时,可以从MDI夫窗体移除锁追加的菜单。
//父窗体中设置 this.文件ToolStripMenuItem.MergeIndex = 1; //子窗体中设置 this.测试ToolStripMenuItem.MergeAction = System.Windows.Forms.MergeAction.Insert; this.测试ToolStripMenuItem.MergeIndex = 1;
11.快捷菜单
ContextMenuStrip控件
12.RichTextBox控件 显示行号 列号
private void rtb_SelectionChanged(object sender, EventArgs e) { t1.Text = "Ln:" + (rtb.GetLineFromCharIndex(rtb.SelectionStart) + 1).ToString(); t2.Text = "Col:" + (rtb.SelectionStart - rtb.GetFirstCharIndexFromLine(rtb.GetLineFromCharIndex(rtb.SelectionStart))).ToString(); }
注:本文整理自《C#编程网络大讲堂》!!!