下面的代码片断假设你的多文档应用程序中包含一个toolbar。当你点击toolBarButton1 按钮时,将会创建并显示一个 Patients 子窗口。下面的代码在单机事件时发生。 private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { bool found = false; if (e.Button == toolBarButton1) { Form[] charr = this.MdiChildren; if (charr.Length == 0) { Patients myPatients = new Patients(); myPatients.MdiParent = this; myPatients.StartPosition = FormStartPosition.Manual; myPatients.Location = new Point(0,0); myPatients.Show(); } else { foreach (Form chform in charr) { if (chform.Name == "Patients") { chform.Activate(); found = true; break; } else found = false; } if (found == false) { Patients myPatients = new Patients(); myPatients.MdiParent = this; myPatients.StartPosition = FormStartPosition.Manual; myPatients.Location = new Point(0,0); myPatients.Show(); } } } }
这样,就实现了一个单窗口实例的解决方案。 
|