How to dispose of a form in the load event?

I have a form that, in the load procedure, tries to find a file.  If it finds it, fine, but if it can't locate the file, I want an error displayed, and the form to never appear.

In my FormLoad procedure, I have :

if filenotfound then
      me.dispose
endif

this results in :
"cannot call dispose while doing createhandle"

Any ideas? Thanks

bobdowdyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

cubixSoftwareCommented:
Hi there

You need to use an intermediate class checks for the existence of the file and if there then it loads your form else simly returns to the calling class.

Something along the lines of


Class Form1

... in here you get to the point to load the next form, but instead call a different class, Say clsFileExists

If clsFileExists.IsFileThere then
     ... load the form you are wanting
End If




The class clsFileExists can be simply

Class clsFileExists

Public Function IsFileThere(byval fileName as string) As Boolean

... code to check for file and return true / false

End Function

End Class


HTH  :)

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
jake072Commented:
You could simply close the form...  Which if you then Dispose of the form from the calling class will Dispose it...

In LOAD:

If FileNotFound Then
    Me.Close()
    Exit Sub
End If

In calling sub:

dim frmName As New frmName
frmName.ShowDialog()
frmName.Dispose()

Hope that helps...

Jake
jjardineCommented:
It is not possible to just call close inside the FormLoad event either.  You will see the same answer.    CubixSoftware's approach is a good way to handle this.    As an alternative to using a helper class you could just add a function to your form that returns whether or not your file exists.  You could then call that before calling your .ShowDialog on it and just never show the form.  


In your Form1 Class

Public Function FileExists() as boolean
  Return True/False
End Sub

From your calling sub:
Dim frmName as new frmName
If frmName.FileExists() Then
  frmName.showDialog()
Else
  frmName.dispose()
End If

Hope that helps.  I ran into this same problem about 2 months ago.  
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Calling Me.Close() from inside the MyBase.Load() event worked for me...
jjardineCommented:
I wonder if it depends on how much is going on in the Form?? or in my situation my forms are MDI children.  I wonder if that causes problems.   I just tried the .close inside of my load and I received an error saying "Cannot call Close() while doing CreateHandle()".

It is interesting that it works in some applications and not others.
cubixSoftwareCommented:
...are we done here?
bobdowdyAuthor Commented:
Yup - were done - implemented CUBIX solution - all is fine - thanks to all we partiicpated !
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.