HOW TO:使用 MenuStrip 建立 MDI 視窗清單 (Windows Form)
請使用多重文件介面 (MDI) 建立應用程式,此應用程式可同時開啟數個文件,並且可複製某一文件的內容並將它貼入另一個文件。
這個程序顯示如何建立父視窗功能表上所有現用子表單的清單。
若要建立 MenuStrip 上的 MDI 視窗清單
建立表單,並將其 IsMdiContainer 屬性設定為 true。
將 MenuStrip 加入表單中。
將子功能表項目加入至 &File 功能表項目,並將該項目的 Text 屬性設定為 &Open。
將 MenuStrip 的 MdiWindowListItem 屬性設定為 &Window ToolStripMenuItem。
將表單加入至專案,並且在其中加入您想要的控制項,例如另一個 MenuStrip。
為 &New ToolStripMenuItem 的 Click 事件建立事件處理常式。
在事件處理常式中,插入類似以下的程式碼,藉此建立和顯示 Form2 的新執行個體,如同 Form1 的 MDI 子系:
Private Sub openToolStripMenuItem_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles _ openToolStripMenuItem.Click Dim NewMDIChild As New Form2() 'Set the parent form of the child window. NewMDIChild.MdiParent = Me 'Display the new form. NewMDIChild.Show() End Sub
[C#]
private void newToolStripMenuItem_Click(object sender, EventArgs e) { Form2 newMDIChild = new Form2(); // Set the parent form of the child window. newMDIChild.MdiParent = this; // Display the new form. newMDIChild.Show(); }
將類似以下的程式碼放入 &New ToolStripMenuItem 之中,以註冊事件處理常式。
Private Sub newToolStripMenuItem_Click(sender As Object, e As _ EventArgs) Handles newToolStripMenuItem.Click
this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click);
編譯程式碼
這個範例需要:
命名為 Form1 和 Form2 的兩個 Form 控制項。
Form1 上名為 menuStrip1 的 MenuStrip 控制項,以及 Form2 上名為 menuStrip2 的 MenuStrip 控制項。
System 和 System.Windows.Forms 組件的參考。