见到很好多问怎么样在父窗体中操作子窗体,就把自己写的贴出来,如对大家有用,最好不过.
下面的代码功能是这样的:在一个父窗体中,有三个子窗体,打开任一个,将关闭之前打开的子窗体,且不重复打开子窗体,也就是任何时候,父窗体中永远只有一个子窗体.
private void 录入信息MToolStripMenuItem_Click(object sender, EventArgs e) { FormAdd fa = new FormAdd(); fa.MdiParent = this; fa.WindowState = FormWindowState.Maximized; for(int i=0;i<this.MdiChildren.Length;i++) if (this.MdiChildren[i] == fa) { fa.Activate(); } else { this.MdiChildren[i].Close(); } fa.Show();
}
private void 查询成绩SToolStripMenuItem_Click(object sender, EventArgs e) { FormSearch fs = new FormSearch(); fs.MdiParent = this; fs.WindowState = FormWindowState.Maximized; for (int i = 0; i < this.MdiChildren.Length; i++) if (this.MdiChildren[i] == fs) { fs.Activate(); } else { this.MdiChildren[i].Close(); } fs.Show();
}
private void FormMain_Load(object sender, EventArgs e) {
}
private void 删除信息DToolStripMenuItem_Click(object sender, EventArgs e) { FormDel fd = new FormDel(); fd.MdiParent = this; fd.WindowState = FormWindowState.Maximized; for (int i = 0; i < this.MdiChildren.Length; i++) if (this.MdiChildren[i] == fd) { fd.Activate(); } else { this.MdiChildren[i].Close(); } fd.Show(); }