I have one form that opens another form upon clicking a button. When the new form opens, I have a progress bar complete via a loop, then I want the form to close. Here is the button click event that launches the new form.
private void calculateButton_Click(object sender, EventArgs e)
{
if (checkFormComplete())
{
ProgressForm proForm = new ProgressForm();
proForm.Show();
}
}
And here is the code for the new form that completes a progress bar that should then close itself.
public ProgressForm()
{
InitializeComponent();
for (int i = 0; i < 101; i++)
calculationProgress.Value = i;
this.Close();
}
However, upon running this I get:
Cannot access a disposed object. Object name: ‘ProgressForm’.
And the debugger points to this line of my main form:
proForm.Show();
I’m not sure I understand why, or what the proper way to do this is. How is that line being called after the close statement inside my new form?
The form is trying to close itself before it’s even shown (because you have your code in the constructor). Put your progress bar code and
Close()call in theFormLoadorFormShownevent instead. Example: