为什么会出现"集合已被修改,枚举操作可能无法执行在c# winform应用程序
本文关键字:操作 执行 应用程序 winform 枚举 quot 修改 集合 为什么 | 更新日期: 2023-09-27 18:11:29
我有两个winforms。frmMainMenu和frmLogin。在frmMainMenu中,我有一个菜单条。其中我保留了一些菜单,其中包括注销选项。所以当我登录时,frmMainMenu出现在前面。我只是不做任何事情,只是点击登出选项frmMainMenu关闭和frmLogin表单再次显示。因此,当点击frmLogin表单的红色十字按钮时,我得到如下异常错误:
应用程序中发生了未处理的异常。如果单击Continue,应用程序将忽略此错误并尝试继续。如果您点击退出,应用程序将立即关闭。
Collection被修改;不能执行枚举操作。
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Restaurant_Management_System
{
public partial class frmMainMenu : Form
{
public frmMainMenu()
{
InitializeComponent();
}
ConnectionString cs = new ConnectionString();
private void productToolStripMenuItem_Click(object sender, EventArgs e)
{
frmProduct frm = new frmProduct();
frm.Show();
}
private void categoryToolStripMenuItem_Click(object sender, EventArgs e)
{
frmCategory frm = new frmCategory();
frm.Show();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
frmAbout frm = new frmAbout();
frm.Show();
}
private void timer1_Tick(object sender, EventArgs e)
{
ToolStripStatusLabel4.Text = System.DateTime.Now.ToString();
}
private void registrationToolStripMenuItem_Click(object sender, EventArgs e)
{
frmRegistration frm = new frmRegistration();
frm.Show();
}
private void loginDetailsToolStripMenuItem_Click(object sender, EventArgs e)
{
// frmLoginDetails frm = new frmLoginDetails();
frmPassVerify frm = new frmPassVerify();
frm.Show();
}
private void profileEntryToolStripMenuItem_Click(object sender, EventArgs e)
{
frmCustomers frm = new frmCustomers();
frm.Show();
}
private void billingToolStripMenuItem_Click(object sender, EventArgs e)
{
frmSales frm = new frmSales();
frm.Show();
}
private void customersToolStripMenuItem_Click(object sender, EventArgs e)
{
frmPassVerCus frm = new frmPassVerCus();
frm.Show();
}
private void salesToolStripMenuItem_Click(object sender, EventArgs e)
{
//frmSalesRecord1 frm = new frmSalesRecord1();
frmPassVerSale frm = new frmPassVerSale();
frm.Show();
}
private void typeToolStripMenuItem_Click(object sender, EventArgs e)
{
frmTypeReg frm = new frmTypeReg();
frm.Show();
}
private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
{
frmInventory frm = new frmInventory();
frm.Show();
}
//I think here is the problem arises. on clicking the logout
private void logoutToolStripMenuItem1_Click(object sender, EventArgs e)
{
frmLogin frm = new frmLogin();
this.Dispose();
frm.Show();
}
}
}
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Restaurant_Management_System
{
public partial class frmLogin : Form
{
ConnectionString cs = new ConnectionString();
DataTable dt = new DataTable();
public frmLogin()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
if (txtUserName.Text == "")
{
MessageBox.Show("Please enter user name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUserName.Focus();
return;
}
if (txtPassword.Text == "")
{
MessageBox.Show("Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPassword.Focus();
return;
}
try
{
SqlConnection myConnection = default(SqlConnection);
myConnection = new SqlConnection(cs.DBConn);
SqlCommand myCommand = default(SqlCommand);
myCommand = new SqlCommand("SELECT Username,password FROM Registration WHERE Username = @username AND password = @UserPassword", myConnection);
SqlParameter uName = new SqlParameter("@username", SqlDbType.VarChar);
SqlParameter uPassword = new SqlParameter("@UserPassword", SqlDbType.VarChar);
//if(txtUserName.Text != "admin")
uName.Value = txtUserName.Text;
uPassword.Value = txtPassword.Text;
myCommand.Parameters.Add(uName);
myCommand.Parameters.Add(uPassword);
myCommand.Connection.Open();
SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
if (myReader.Read() == true)
{
int i;
ProgressBar1.Visible = true;
ProgressBar1.Maximum = 5000;
ProgressBar1.Minimum = 0;
ProgressBar1.Value = 4;
ProgressBar1.Step = 1;
for (i = 0; i <= 5000; i++)
{
ProgressBar1.PerformStep();
}
this.Hide();
if (txtUserName.Text == "admin")
{
frmMainMenu frm = new frmMainMenu();
frm.Show();
frm.lblUser.Text = txtUserName.Text;
}
if (txtUserName.Text != "admin")
{
frmMainMenu1 frm = new frmMainMenu1();
frm.Show();
frm.lblUser.Text = txtUserName.Text;
}
}
else
{
MessageBox.Show("Login is Failed...Try again !", "Login Denied", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUserName.Clear();
txtPassword.Clear();
txtUserName.Focus();
}
if (myConnection.State == ConnectionState.Open)
{
myConnection.Dispose();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Form1_Load(object sender, EventArgs e)
{
ProgressBar1.Visible = false;
txtUserName.Focus();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.Dispose();
Application.Exit();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Hide();
frmChangePassword frm = new frmChangePassword();
frm.Show();
frm.txtUserName.Text = "";
frm.txtNewPassword.Text = "";
frm.txtOldPassword.Text = "";
frm.txtConfirmPassword.Text = "";
}
private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Hide();
frmRecoveryPassword frm = new frmRecoveryPassword();
frm.txtEmail.Focus();
frm.Show();
}
}
}
如何处理这个异常?我不想在关闭时看到这个错误。提前感谢:)
假设下面的事件附加到LogOut按钮上。删除/评论this.Dispose ();行
private void logoutToolStripMenuItem1_Click(object sender, EventArgs e)
{
frmLogin frm = new frmLogin();
this.Dispose(); //Comment This Line
frm.Show();
}