Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

Can I cancel the Form Load event ?

I have some logic in the form load event handler that, in
some cases, make the form unnecessary. So, I tried setting
the Dislogresult to OK - the form is still displayed.
I added Me.Close - same thing. I tried
these two in the Activated event - no difference.
(I am showing the form using ShowDialog method).

Do DialogResult and/or Close not work in Load/Activate
event handlers ? What is the "proper" way to stop the
form from being loaded/displayed and return a dialog
result ?

If you need the specifics, here you go: in form load,
I am looking for records that match the search criteria; if I
find > 1, I shod them in the form's listbox; if I only find 1,
I'd like to not show the form at all and just return the ID of
the only record that was found (in a public property of
the form).

Thanks in advance.

Saturday, March 22, 2003

If you have some logic that may throw an exception or otherwise invalidate the need to show the form then it should be done prior to attempting to show the form. Put it in the constructor and throw an exception that you catch in the calling procedure or create a custom Initialize method that must be successfully completed prior to showing the form.

In your case you probably want to set up a routine (either in your form class or another) that returns an ID. There are numerous ways of doing this depending on your particular circumstances but here's an example of what I mean:

Public Function FindMatch(SearchCriteria) as Integer
  'Do sql using SearchCriteria

  'If no records found throw exception or return invalid ID
  'If one record found return its ID

  'If multiple records found then use DataSet to initialize the list in the form and then ShowDialog the form.

  'If user cancels without selecting a record then throw exception or return invalid ID

  'If user selects item then return that item's ID.

End Function

Stephen Martin
Sunday, March 23, 2003

Thanks for the reply. So, the answer to the thread question is "yes, but only by throwing an exception", correct?

Sunday, March 23, 2003

No, the answer is no. Do the logic before you try to load the form and then only show the form if it is needed and everything has gone as you expected.

Stephen Martin
Sunday, March 23, 2003

*  Recent Topics

*  Fog Creek Home