Question About Form_Closing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mchuck

    #1

    Question About Form_Closing

    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.Compone ntModel.CancelE ventArgs". In VB6, the
    same function (with a different name)
    called "Form_QueryUnlo ad" had one parameter
    called "Unload" passed in that you were able to tell who
    executed the order to close the form ("vbFormControl Menu"
    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!!!
  • Steve McLellan

    #2
    Re: Question About Form_Closing

    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

    "Mchuck" <mchuck@hotmail .com> wrote in message
    news:0b3e01c47a fd$32eaf4f0$a40 1280a@phx.gbl.. .[color=blue]
    > 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.Compone ntModel.CancelE ventArgs". In VB6, the
    > same function (with a different name)
    > called "Form_QueryUnlo ad" had one parameter
    > called "Unload" passed in that you were able to tell who
    > executed the order to close the form ("vbFormControl Menu"
    > 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!!![/color]


    Comment

    Working...