ObjectDisposedException when Multiple Forms Are Open
My C# Winform application encounters the situation where it cannot access a disposed object. The disposed object is a form (frmQuiz) that is opened from a button on the login form.
The Situation:
My application typically has two or three forms open at the same time. The Program.cs file runs form frmLoginBackground, which is just a semi-transparent background that covers the computer screen. The load event for this form opens the second form, frmLogin, which includes a button that opens frmQuiz, which is a simple form with a few math questions on it.
The code in frmLogin that opens frmQuiz looks like this:
private void btnTakeQuizNow_Click(object sender, EventArgs e)
{
frmQuiz quiz = new frmQuiz();
quiz.TakeQuizNow("take_quiz_now", Convert.ToInt32(comboQuizMeNow.SelectedValue)); //Pass the form a quiz id number.
quiz.Show();
}
When frmQuiz opens both it and frmLogin are open and accessible.
The frmLogin also contains a password control that opens the administration form by first opening frmSplash, which is a "Please Wait..." splash form based on a timer. The timer Tick event launches frmAdmin, which is the administration form. The code in frmLogin looks like this:
private void btnPasswordSubmit_Click(object sender, EventArgs e)
{
//Password verification code snipped.
frmSplash objSplash = new frmSplash();
objSplash.Show();
//this.Hide();
this.Close();
}
And the code in frmSplash looks like this:
private void timer1_Tick(object sender, EventArgs e)
{
frmAdmin objfrmAdmin = new frmAdmin ();
objfrmAdmin.Show();
this.Close();
}
When frmAdmin opens then frmLogin is no longer accessible; however, frmAdmin contains a 'Return to Login Screen' button with code like this:
private void btnReturnToLogin_Click(object sender, EventArgs e)
{
exitWarnings("return_to_login");
}
private void exitWarnings(string action)
{
//Warning message code snipped.
开发者_如何学C if (action == "return_to_login")
{
frmLogin objLogin = new frmLogin();
objLogin.Show();
}
}
The frmLoginBackground remains open until the application exits.
The Problem:
Everything works fine when frmLogin first opens and the button is clicked to open frmQuiz. The quiz form opens a runs fine. However, after logging into the administration form (which closes or hides the login form) and then clicking the 'Return to Login Screen' link, then, after frmLogin reappears, the object disposed exception occurs when clicking the button to open frmQuiz. Visual Studio highlights in yellow the "quiz.Show();" line of code. The exception occurs regardless of weather I use "this.Close();" or "this.Hide();" in the btnPasswordSubmit_Click event.
Can anyone suggest a solution that allows me to open frmQuiz after returning to frmLogin from frmAdmin.
Cheers, Frederick
Since you create a new instance for quizz
just before the quizz.Show()
it cannot be quizz itself that throws the exception.
Take a good look at the constructor and FormCreate event of frmQuiz. It looks like that is where the dead horse is being kicked.
精彩评论