Object reference not set to an instance of an object

I have a view form which loads a instance form, here is the code. The problem is the user can close the main form. Is there anything I can check before closing the main form to see if any new forms have been spawned from it.

       Dim frm As New UserForm
                AddHandler frm.Closed, AddressOf InstanceFormClosed
                Me.Cursor = Cursors.Default
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There is a windows API called enumChildWindows that you might be able to call... I used it in an example to enumerate all the controls on a given form, but it might also work for windows spawned from another window... worth a try.  This example code is in VB 6, but you can probably cut and paste into .NET


Joel CoehoornDirector of Information TechnologyCommented:
Try doing it this way:

Dim frm As New UserForm(Me)
AddHandler frm.Closed, AddressOf InstanceFormClosed
Me.Cursor = Cursors.Default

Then you can look at Me.OwnedForms to see a list of forms that were opened with this code.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.