0

I have multiple forms in my program. The menu form (frmMenu) contains buttons that will hide the menu form and show their corresponding form e.g. form frmXyl. Closing the form frmXyl should hide it and show the form frmMenu.

'Form frmMenu button click event

    'Hide the form frmMenu
     Me.Hide()
    'Show the form frmXyl
     frmXyl.Show()

'This is the form frmXyl form closing event

    'Hide the form frmXyl
    Me.Hide()
    'Show the form frmMenu
    frmMenu.Show()

I can start the program and click on the button to hide frmMenu and show frmXyl, then I can close frmXyl which hides frmXyl and show frmMenu.

Now, if I click the button to show frmXyl and hide frmMenu a second time I get an error.

Here is the error: An error occurred creating the form. See Exception.InnerException for details. The error is: COM object that has been separated from its underlying RCW cannot be used.

I have used show and hide with forms before without issue, thanks in advance of any solution you may have to this.

  • What's the InnerException stack trace? Are you using Office? – SLaks Sep 05 '12 at 13:27
  • How do you close the second form? Processing the Form_Closing event ? – Steve Sep 05 '12 at 13:30
  • Can you tell me where I'd find the stack trace? Office is installed but not being used with this program nor is it running. Worth noting perhaps that I am using AxWindowsMediaPlayer cotrols and the imports System.IO, AxWMPLib, WMPLib. Yes Steve, I am using the form closing event. The problem occurs when I try and show the form frmXyl a second time. – user1649142 Sep 05 '12 at 13:32

1 Answers1

1

Hiding a form is not enough to prevent it from getting disposed. You also need to set e.Cancel = True in the FormClosing event to allow the form object to survive. You'll need to pay attention to the e.CloseReason, you don't want to stop it from closing and display a new form when, say, Windows is shutting down or the user tries to exit your program.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Do I need to dispose the form if I just want it to be hidden until it is needed again? The program can only be closed from the form frmMenu. Incidentaly I am only getting this error with one of my forms, the others all function (show/hide) and ***do not*** include e.Cancel = True – user1649142 Sep 05 '12 at 13:40
  • You do *not* want it disposed if you want to show it again. The ActiveX control gets particularly cranky about it. Part of the problem is that you are using the awful vb.net syntax for forms, frmMenu is a *type name*, not a variable name. – Hans Passant Sep 05 '12 at 13:45
  • frmMenu is the name of one of the forms in the project, I never said it was a variable?! Thanks for pointing out e.Cancel = true. I'd used it before but didn't realize I needed it here. – user1649142 Sep 05 '12 at 13:53