Cancelling form load event

L

lgbjr

Hi All,

I see that this topic was discussed back on august 23rd, but I didn't see a
real answer. I have a VB.NET MDI app. A particular menu action on the MDI
parent is to call an OpenFileDialog box. If the result is ok, I show a form
with related information about the file.

However, there are only specific files allowed. In the MDI child form load
event, I check the validity of the file (right version, not open in another
app, etc.). Based on a post I saw from Jay (repeated in the thread here on
august 23rd), I throw an exception if there's a problem with the file.

Here's some code. first, how I call the form (in the MDI Parent):

Protected Sub MDICDLNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Menu_CDL_New.Click
Dim result As DialogResult = CDL2X_CDL_Pick.ShowDialog()
If result = DialogResult.OK Then
MainMDI.ActiveForm.Refresh()
CDL.CDLfile = CDL2X_CDL_Pick.FileName
winnum = winnum + 1
Try
Dim NewCDLwin As New Analyze
NewCDLwin.Text = NewCDLwin.Text + " " + winnum.ToString
NewCDLwin.MdiParent = Me
NewCDLwin.Show()
Catch ex As Exception
winnum = winnum - 1
MessageBox.Show(ex.Message, "Open CDL Failure",
MessageBoxButtons.OK, MessageBoxIcon.Error)

End Try
End If
End Sub

In the child form, I check a bunch of stuff about the file, and if it isn't
the right type of file, or the wrong version, or if it's open in another
app, I throw an exception in various locations throughout the form load
code:

Throw New Exception(CDL2X_CDL_Name.Text + " is not a supported CDL
Version!")

However, the form still loads! What am I missing? Based on Jay's post on
PCReview and again here, throwing an exception should cancel the load event,
but it doesn't.

any thoughts?

TIA
Lee
 
C

Chris Dunaway

lgbjr said:
In the child form, I check a bunch of stuff about the file, and if it isn't
the right type of file, or the wrong version, or if it's open in another
app, I throw an exception in various locations throughout the form load
code:

Probably the simplest solution to this problem is "Don't show the form
unless you need to!

Check all that stuff about the file before you show the child form:

If FileIsValid() Then
'show child form here
Else
MsgBox("invalid file")
End If
 
L

lgbjr

Hi Chrix,

In general, I agree, and I think that's what I'm going to have to do. In my
app, I have several different child forms, each with quite a few controls
and lots of code to perform various tasks associated with different types of
files. My goal was to keep all of the logic for each type of document in the
child form associated with the document type, but it looks ike I'm going to
have to move at least the initial verification stuff to the MDI parent.

Still, it seems to me that if an exception is thrown in the load event, the
form shouldn't load.

Cheers,
Lee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top