#region Using directives using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; #endregion namespace SingleMDISample { partial class MDIParentForm : Form { [DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd); public MDIParentForm() { InitializeComponent(); } static public int FormCount = 0; public void CreateMDIChildWindow(string fileName) { // This is necessary to bring the MDI parent window to the front, // since Activate and BringToFront don't seem to have any effect. SetForegroundWindow(this.Handle); // Detect if file is already open if( !string.IsNullOrEmpty(fileName) ) { foreach( MDIChildForm openForm in this.MdiChildren ) { if( string.Compare(openForm.FileName, fileName, true) == 0 ) { openForm.Activate(); return; } } } // If file not open, open it MDIChildForm form = new MDIChildForm(); form.OpenFile(fileName); form.MdiParent = this; form.Show(); } private void newToolStripMenuItem_Click(object sender, EventArgs e) { this.CreateMDIChildWindow(null); } private void openToolStripMenuItem_Click(object sender, EventArgs e) { if( this.openFileDialog.ShowDialog() == DialogResult.OK ) { this.CreateMDIChildWindow(this.openFileDialog.FileName); } } } }