Winform Collection was modified; enumeration operation may not execute
本问题已经有最佳答案,请猛点这里访问。
我的代码是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | private void studentToolStripMenuItem_Click(object sender, EventArgs e) { foreach (Form frm in Application.OpenForms) { if (frm.Text =="student") { frm.Activate(); } else { studetn obj = new studetn(); obj.MdiParent = this; obj.Dock = DockStyle.Fill; obj.Show(); } } } |
show()命令修改正在枚举的application.openforms集合。这是不允许的。您应该在foreach循环之外创建和显示新表单:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | private void studentToolStripMenuItem_Click(object sender, EventArgs e) { // Find the student form Form frmStudent = null; foreach (Form frm in Application.OpenForms) { if (frm.Text =="student") { frmStudent = frm; break; } } // if found - activate, if not found - create if(frmStudent!=null) { frmStudent.Activate(); } else { studetn obj = new studetn(); obj.MdiParent = this; obj.Dock = DockStyle.Fill; obj.Show(); } } |