views:

150

answers:

2

[Edit]
See this post as to why I'm declaring form elements globally.

I chose to rewire my code to get rid of the global definitions.
[End Edit]

I'm creating a from dynamically at runtime. the form, all buttons, and the combobox are all declared globally to the parent form.

dim myForm as new form

I put some buttons on it and a combo box that I'm filling with stuff.

I can show the form fine with

myForm.show()
myForm.bringToFront()

everything works fine the first time.

when the user is finished with the from, I call:

myForm.close()
'have also tried
' myForm.Hide() and myForm.Dispose()

either way, (even when only calling .close()) the second time the form is created, I get the following error:

ObjectDisposedException was unhandled Cannot access a disposed object.

If I just toggle the visibility of the form, the combobox values aren't wiped out each time - and it seems like sorry practice to just set visibility = false.

why is this happening?

+1  A: 

Because once your form is closed, it can't be reopened.

Each time you want to open a new form, you'll have to reassign it to a new instance of a form. If you do want the same instance, myForm.Show() and myForm.Hide() should work, as long as you never call myForm.Close()

Brandon
see my edit. I am re-instantiating the form each time.
42
A: 

I guess your problem is here: "the form, all buttons, and the combobox are all declared globally to the parent form."

You are defining the form globally so the garbage collector will not clear it automatically as it is always on scope.

Either you don't dispose the of the form or you stop using it as a global variable.

Sergio