I hoping that someone might be able to help me with a problem I can't get figured out. Let me described the issue:
I'm developing an application that has a main menu (actually a form) that opens multiple forms via buttons on the form itself. I have no problems opening a form from the main menu and closing them. However, if I try to re-open a form from the main menu that I have already opened and closed, an "ObjectDisposedException" is thrown saying it can't re-open an already disposed object. I believe this to be an issue with the garbage collection, but I can't seem to find the right information to get this corrected.
Any ideas on how to correct this problem so I can open, close, and re-open a form multiple times from the same control Is it an issue with a property of the form itself that needs to be changed from its default Any information would be greatly appreciated!
Thanks in advance for the help,
Tony

Object Disposed Exception Experienced When Re-opening Forms
DaveV
private Form1 _form1; private void button1_Click(object sender, EventArgs e)
{
if (_form1 == null){
_form1 =
new Form1();}
_form1.Show();
}
The 2nd time you click the button, the local reference is not null, but the form has been disposed (because it was closed), and so you are trying to show a disposed form.
You have a couple of choices. You can hide the form instead of closing it (in Form1.Closing, do this.Hide() and set e.Cancel = true). This means you are reusing the same form everytime you click the button.
You can not keep a local variable around in your main menu, and just open a new form every time. This means you could have multiple instances of Form1 displayed at any one time.
Or, you can check all your open forms and see if one of them is Form1, and if so, bring it to the front. Otherwise, new up an instance. Like so:
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = null; foreach (Form f in Application.OpenForms){
if (f.GetType().IsAssignableFrom(typeof(Form1))){
form1 = (
Form1) f; break;}
}
if (form1 == null){
form1 =
new Form1();}
form1.Show();
form1.BringToFront();
}
VC_Mike
Thanks very much for the post. I really appreciate it. I'm going to try a couple of tweaks with your code and see what I come up with.
What I discovered on my own was when I took out the variable,
Dim trngForm as New TrainingForm
the Button1 click event to show this form,
Private
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickTrainingForm.Show()
End Substill showed the form and I could open and close it multiple times without getting the ObjectDisposedException. I'm new to programming and I guess the variable statement created a conflict. Gotta love the learning curve. Hope this makes sense.
Thanks again,
Tony