?
Solved

How to dispose of a form in the load event?

Posted on 2006-03-23
7
Medium Priority
?
579 Views
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
      me.dispose
endif

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

Any ideas? Thanks

0
Comment
Question by:bobdowdy
7 Comments
 
LVL 6

Accepted Solution

by:
cubixSoftware earned 1000 total points
ID: 16268771
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  :)
0
 
LVL 14

Expert Comment

by:jake072
ID: 16269243
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
0
 
LVL 14

Expert Comment

by:jjardine
ID: 16271164
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.  
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

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

Expert Comment

by:jjardine
ID: 16271869
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.
0
 
LVL 6

Expert Comment

by:cubixSoftware
ID: 16309227
...are we done here?
0
 

Author Comment

by:bobdowdy
ID: 16397461
Yup - were done - implemented CUBIX solution - all is fine - thanks to all we partiicpated !
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

840 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