Question About Form_Closing

  • Thread starter Thread starter Mchuck
  • Start date Start date
M

Mchuck

OK, my question has to do with identifing who sent the
order to close a form. Basically, when the code goes into
the Form_Closing event, there are two parameters passed
in: "sender" as an "Object" and "e" as
a "System.ComponentModel.CancelEventArgs". In VB6, the
same function (with a different name)
called "Form_QueryUnload" had one parameter
called "Unload" passed in that you were able to tell who
executed the order to close the form ("vbFormControlMenu"
if the user clicked the "X" button of the form). However,
in VB.NET, those two new parameters are not even close to
holding the information that I want. Maybe they do, but
God knows how to extract the right info from them.

I've found numerous responses to this problem off of other
VB forums, but they all end up with the same solution: use
StackTrace's to find the sender of the closing event. And
it seems to work fine, but at the same time it uses
two "magic numbers": 6 and 7, to represent two stackframes
with the closing information in it. Now, if this is
DEFINATELY the only way to do this, and those magic
numbers will never change and forever stay the same on
anyone's machine, then I'd be OK putting it in the code.
But if there is a way that anyone knows of other than
stacktracing to figure this out (to avoid damn magic
numbers!!), please post a very welcomed response!!!!
Thanks in advance guys!!!
 
Hi,

If you're trying to execute certain code when the user closes the form
through the X button or a menu option, and distinguish that from a
programmatic close event, one way to do it is to have a boolean variable in
the form that allows the Close code to execute if it's not set. Any
programmatic shutdown can call a function which sets that flag and then
calls Close. It's not a great method, but it sure sounds better than relying
on magic numbers in stacktraces if that is the case.

Steve
 
Back
Top