Answered by:
How does one deal with the "Cannot acces a disposed object" error?

Question
-
My program involves accessing a print preview dialog. When a button is clicked, the code is:
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.Show()It works just fine the first time. However, when I close the printpreviewdialog and then try to open it again (through the button) in the same run then I get a "cannot access a disposed object" runtime error. Hence, it can only work once during a program run and I need it to do more.
How can this problem be fixed?
Sunday, February 26, 2012 12:17 AM
Answers
-
PrintPreviewDialog is a dialogue box - the clue is in the name.
Therefore it should be shown using ShowDialog.
Then when you close it it doesn't close, it just hides.
- Marked as answer by PRS99 Sunday, February 26, 2012 12:46 AM
Sunday, February 26, 2012 12:36 AM
All replies
-
Hi,
Do not dispose of the PrintPreviewDialog at any point in your code and your problem will be solved I reckon. :)
Can you post your code to show where you are getting the error?
If that is all you have you could do this.>>
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If PrintPreviewDialog1 Is Nothing Then PrintPreviewDialog1 = New PrintPreviewDialog End If PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.Show() End Sub End Class
Regards,
Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.
- Edited by John Anthony Oliver Sunday, February 26, 2012 12:28 AM
Sunday, February 26, 2012 12:21 AM -
What is the object that has been disposed? When the error occurs, hover the mouse over each object referred to in that line of code. Which ones are reported as available objects and which ones are reported as Nothing?
- Proposed as answer by Frank L. Smith Sunday, February 26, 2012 12:25 AM
Sunday, February 26, 2012 12:23 AM -
What is the object that has been disposed? When the error occurs, hover the mouse over each object referred to in that line of code. Which ones are reported as available objects and which ones are reported as Nothing?
That's what I was about to type - I think he's confusing what's disposed.
I'll be curious so see what he finds on this.
Sunday, February 26, 2012 12:26 AM -
PrintPreviewDialog is a dialogue box - the clue is in the name.
Therefore it should be shown using ShowDialog.
Then when you close it it doesn't close, it just hides.
- Marked as answer by PRS99 Sunday, February 26, 2012 12:46 AM
Sunday, February 26, 2012 12:36 AM -
PrintPreviewDialog is a dialogue box - the clue is in the name.
Therefore it should be shown using ShowDialog.
Then when you close it it doesn't close, it just hides.
Why not create it then intentionally dispose it, ergo "Using ppd As New PrintPreviewDialog"?
Then in the Using block, use ".ShowDialog"? That's what came to my mind, but in all fairness, I don't use it - I have third party stuff that does all that for me but unless it's intrinsically different ...?
Sunday, February 26, 2012 12:41 AM -
That's what I would do but if you add one to the form from the toolbox, show it using .Show and then click the close button it behaves like any other form and disposes of itself. So next time you try you get the exception because, unlike other forms it doesn't auto instantiate.
Question is why have they coded it like this when all the other dialogues only have a ShowDialog method and not a .Show
- Edited by Dave299 Sunday, February 26, 2012 12:48 AM
Sunday, February 26, 2012 12:47 AM -
That's what I would do but if you add one to the form from the toolbox, show it using .Show and then click the close button it behaves like any other form and disposes of itself. So next time you try you get the exception because, unlike other forms it doesn't auto instantiate.
Question is why have they coded it like this when all the other dialogues only have a ShowDialog method and not a .Show
Right, the same as a FileDialog.
@PRS-> Reconsider how you're doing this. Despite that you've found your answer, I think that in the long-run you'll find it more helpful to create it in code, instantiate a new one each time, and do it all in a Using block so that you're intentionally completely doing away with all resources associated with it once used.
It's not difficult at all but if you need help, just ask.
Good luck with your project. :)
Sunday, February 26, 2012 12:53 AM