How to open a form multiple times? I have this event:
Form2 myForm = new Form2();
private void button_Click(object sender, EventArgs e)
{
myForm.Show();
}
When I debug my project with VisualStudio 2008, the first time I clicked on the button, the Form was Showed, but when I closed it, and I tried to open it again, i get an error similar to this: Impossible to access an eliminated object. Object Name: ‘Form2’.
Can anyone explain this behaviour to me?
You can also override
Form2Closingevent, interrupt it and callHide()method instead. This way, you don’t have to create new instance everytime you want to show your window.Edit:
Here’s example of question on Stackoveflow explaining this method. What you’ll have to do, is when creating original instance of your Form2 class, hook up to its closing event by adding following code to the Form2 class:
And that’s all. You don’t have to change your
button_Clickhandler.