How to dispose of a form in the load event?

Posted on 2006-03-23
Last Modified: 2008-03-10
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

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

Any ideas? Thanks

Question by:bobdowdy
    LVL 6

    Accepted Solution

    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  :)
    LVL 14

    Expert Comment

    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
        Exit Sub
    End If

    In calling sub:

    dim frmName As New frmName

    Hope that helps...

    LVL 14

    Expert Comment

    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
    End If

    Hope that helps.  I ran into this same problem about 2 months ago.  
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Calling Me.Close() from inside the MyBase.Load() event worked for me...
    LVL 14

    Expert Comment

    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.
    LVL 6

    Expert Comment

    ...are we done here?

    Author Comment

    Yup - were done - implemented CUBIX solution - all is fine - thanks to all we partiicpated !

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now