I'm using VB.net 2008...

Within the program, there is a button that opens a file inside of a second form ( DocViewFrm ) and it works fine the first time around. Within this new window, there is a button that uses Me.Close() to close the window and return to the main window. Upon the attempt to open this second form a second time, the program crashes on the Me.DocViewFrm.Show() line. The error message reads:

An unhandled exception of type 'System.ObjectDisposedException' occurred in System.Windows.Forms.dll
Additional information: Cannot access a disposed object.

What method must be used to close this window properly so that I can open another one later? Any help is MUCH appreciated!

Recommended Answers

All 9 Replies

this DocViewFrm dont exist anymore when you close it.
so each time you need that window do:

dim frm as new DocViewFrm
frm. show

The main window stays open throughout the use of the second window. When putting the frm.close line in, the window closes immediately after it is opened. The form is = Me before it is opened. Does Me.Close not work for this?

Sorry if the questions sound simple, but I'm a novice... I just don't know.

yes, the Me.Close will work in that second form ...
my example just showed how to close that form from the main form.

I am using Me.Close in the DocViewFrm already. This does not work though. The form will disappear from the screen, and the application does not show up in the task manager after close, yet the error still comes up. For some reason, it seems to not be closing, thus not allowing the opening of a second document. Is there some kind of background thread running that needs shutdown?

Me.Close actually exits the program. Use Me.hide() instead.

Okay... it is probably my fault, but it seems my problem is not being understood.

In Form1, there is a button (OpenDoc) that opens DocViewFrm and hides Form1. In DocViewFrm, there is a button (Menu) that closes DocViewFrm and shows to Form1. Upon running the program, the user clicks OpenDoc button and DocViewFrm appears. Some work is done, and then the Menu button is clicked, closing DocViewFrm and returning to Form1. To this point, there has been no error. If we click OpenDoc again, the program stops on the line Me.DocViewFrm.Show() and says
"An unhandled exception of type 'System.ObjectDisposedException' occurred in System.Windows.Forms.dll
Additional information: Cannot access a disposed object."
When the program is run again, the same condition exists.
Right now, the Menu button in DocViewFrm uses Me.Close(). I have tried Me.Hide, but when the form is opened again, the view is the same as the first time it opened. This method merely hides and shows the form without making any changes.
I even tried Me.Unload, but it left me in the same place. I have tried a few different variations of opening/closing with no luck. I thought that by leaving it for a few days, and returning that it might click in my head, but it just frustrates me more now than it did before. I have moved on to working on other parts of this program for now. This issue still needs solved.
One condition I must satisfy (that I have not yet attempted) is that it must be able to open multiple windows.

ANY help is VERY much appreciated. I said it before, but I'm new at this stuff, and my supervisors seem to think I'm the man for the job. :/ Hopefully someone here can help me with this.

Thank you!

Member Avatar

GeekByCoiCe has written your solution.

When you show the form the first time it is initialized because you had a line somewhere that said dcvwform as new docviewform. After you close the docviewform with Me.close, "docviewform.close", the dispose method is also being called on the docviewform. Meaning it is now disposed. If you try to show it again with out creating another instance of the docviewform your going to keep getting the exception.

Before each line where you have


you should have

dcvwform = new docviewform

A better way to handle your dock view form is to not hide the main form but instead show the docviewform as a dialog form.

dcvwform = new docviewform

' send anthing you need to send to the docviewform

' get anything you need to get from the docviewform


But thats entirely up to you.

Correct me if I am wrong, but DocVeiwForm is a seperate form, no?

Sh why are you using Me.DocViewFrm.Show() ?

It should just be DocViewFrm.Show()

And you can open multiple forms with something like this (untested)

'This goes in your main form class
Dim WindowArray(0) as DocViewFrm

And then to actually show mutliple windows, you do this:

Redim Preserve WindowArray(Ubound(WindowArray)+1)
Dim M as long = ubound(WindowArray)
WindowArra(M) = new DocViewFrm

You guys are correct. After checking my code more carefully, I had forgotten the DocViewFrm = New DocView. After typing the three characters, this portion of the program works great.
Sorry I misunderstood everyone! Now I see and understand though. Thanks again for all of your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.