By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

Cancelling form load event

P: n/a
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


Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
lgbjr wrote:
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

Nov 21 '05 #2

P: n/a
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

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
lgbjr wrote:
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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.