none
'System.ObjectDisposedException' occurred in System.Windows.Forms.dll

    Question

  • I am trying to use the print window non-modal: ppd.Show()

    (Dim ppd As PrintPreviewDialog)

    It is working Ok the first time, the second time I get:

    A first chance exception of type 'System.ObjectDisposedException' occurred in System.Windows.Forms.dll

    When I use it modal it is working fine: ppd.ShowDialog()

    Please, what is the difference here and what should I do?


    Appleforce

    Tuesday, October 09, 2012 1:22 PM

Answers

  • My clairvoyance is failing me this morning, but try passing the PrintPreviewDialog you want to use to the InitializePrintPreviewDialog method.

    • Marked as answer by appleforce Wednesday, October 10, 2012 2:24 PM
    Wednesday, October 10, 2012 10:13 AM

All replies

  • The first time you must create an instance of it somewhere but not the second time.  You should modify your code to ensure that you have an instance of the PrintPreviewDialog every time. 
    Tuesday, October 09, 2012 1:36 PM
  • Public ppd As PrintPreviewDialog = myMDI.MDIPrintPreviewDialog

    I thougt I already did through the my public statement.

    And it is working fine every time with ShowDialog()


    Appleforce


    • Edited by appleforce Tuesday, October 09, 2012 2:29 PM
    Tuesday, October 09, 2012 2:28 PM
  • If that's the statement that causes the error, then myMDI.MDIPrintPreViewDialog is being disposed somewhere in your code.

    Tuesday, October 09, 2012 3:02 PM

  • Please, what is the difference here and what should I do?



    A form showed with Show is somewhere going in the OS absolute without control. Normally a form is started with Application.Run(TheFormName). In VB this is hidden in the application framework but it happens still like that. 

    Try to avoid forms which are showed with Show, with the exception with forms like splash screens which only are showed and are disposed by there own timer at a certain moment. 

    An alternative are the by John named MDI forms where the control is done by the Multiple Document Interface.

    Otherwise use panels, groupboxes, tabpages, sliders or all those features which are much better in this for instance with user controls.


    Success
    Cor

    Tuesday, October 09, 2012 3:19 PM
  • As Cor alludes to, when you show a form by calling the Show method, when the form is closed, it is automatically Disposed.  You can then no longer use the form and will have to "new up" a new one.

    However, if the form is shown by calling ShowDialog it is *not* automatically Disposed when it is closed, you must call Dispose to dispose of the form.  But until the form is Disposed, you can still use it.

    So as John indicated, if you Close the form, you must re-instantiate it before you can use it again.


    Tuesday, October 09, 2012 9:38 PM
  • OK, thanks for your clarifying answers, Chris, Cor and John, I understand what you are saying, but I don't understand how to do that, that is reinitiate the very special form PrintPreviewDialog.

    I have a declaration on module level:

    Public ppd As New PrintPreviewDialog 

    and in the sub

    InitializePrintPreviewDialog()

    I have

    ppd = myMDI.MDIPrintPreviewDialog
    and in my form myMDI I have added from the Toolbox a PrintPreviewDialog called MDIPrintPreviewDialog .

    But appareantly this is not enough. What am I missing?




    Appleforce

    Wednesday, October 10, 2012 10:08 AM
  • My clairvoyance is failing me this morning, but try passing the PrintPreviewDialog you want to use to the InitializePrintPreviewDialog method.

    • Marked as answer by appleforce Wednesday, October 10, 2012 2:24 PM
    Wednesday, October 10, 2012 10:13 AM
  • OK, thanks John, I tried passing the PrintPreviewDialog to the InitializePrintPreviewDialog method and with a little use of the famous method 'trial-and-error' I really succeded in making it work. I had to get rid of the PrintPreviewDialog from the Toolbox.

    And as an extra benefit it is now possible to have open several Printwindows. I didn't even know that this was possible, but it is and I like it!

    Thanks again!


    Appleforce

    Wednesday, October 10, 2012 2:23 PM