Validating and Closing a Form

Ok, I give.  If you set e.Cancel = true in a validating event (for a textbox for example), how can you make it so the form can be closed?  Like clicking the X or even calling me.Close.

I think I'm slowly losing my mind.

Edit: Chris Kinsman's solution, while pretty snazzy, is just plain silly to have to resort to these shenanigans. 

Man, I can't wait for Chris Sells' book.

13 Comments

  • That still doesn't solve the problem of the me.close. I mean, the Validating events were supposed to be there to solve the problem with the LostFocus/SetFocus issue. I'd rather deal with that than this crap.





    Plus, I'm not trying to get it to work, I'm trying really to figure out if this is intended operation, and if so why. I'm not a big fan of restricting a user's input order, so I stick to just showing errors and disabling Ok buttons.

  • It's intended operation, at least in .net 1.0. I've asked this last year in the newsgroups. It's completely unnecessary that the form calls the validators, but MS decided otherwise. I don't know if its different in .NET 1.1, but I doubt it.





    MS didnt give a reason why the validator is called in the close operation though.

  • Thanks for your help here Frans. I tried it in 1.1 and same behavior. I guess I need to update the courseware I teach from to "don't use the cancel and here's why."





    Sigh.

  • You need to put some logic in your validating event to figure out what caused validation to be fired. The usual solution to allow the close box is to check the mouse position. If the mouse is inside the client area of the form then the user navigated to another control; if not then the user probably clicked the close box. Just don't do the validation if the mouse is not inside the client area.





  • Hi guys,

    I am using windows data grid. I have windows application in VB.net. I have a validate event written for grid. But when i close my form, validate event is NOT fired..how to handle this..?. My fomr is modal. Can any one help..me.

    Do reply me at clara.paul@vizual.co.in

    Regards,

    clara

  • You can always use reflection to check from where the validation is originating from.



    protected bool formIsClosing()

    {

    System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();

    for(int i = 2; i < stackTrace.FrameCount; i++)

    {

    if(stackTrace.GetFrame(i).GetMethod().Name == "WmClose")

    {

    return true;

    }

    }

    return false;

    }



    Regards

    /T

  • that was wat exactly i was searching for... thanx buddy .....

  • Hi guys. how to close with valid w3c
    here (kampung rm3.50
    )
    Thanks for help.

  • Joshua has that backwards. You need to set

    e.Cancel = false;

    Otherwise, it cancels the closing of the form.

  • The only problem I have is that when you try to close a form, you can't if any child controls are invalid (from validating event)

  • The solution of implementing e.Cancel = false in the Form.Closing event doesn't work for me, neither. The problem is, if you set e.Cancel = true in the validation event, the focus sticks to the textbox. You can't hit ESC or the Cancel button. I mean, you can, but it has no effect, the Canceling event won't be fired. So the code in there doesn't make any difference, because it's never reached.

    That's a real pitty, making e.Cancel = true in the validation event pretty much useless :(

    - Regards
    - spitfire

  • Found a working solution: Set the button property "CausesValidation" to false. Then - assuming the Form.Closing event is implemented as described above, you can close the form using your custom button, even when the focus is locked on a textbox due to validation. :)

  • R. Bemrose said:

    Joshua has that backwards. You need to set

    e.Cancel = false;

    Otherwise, it cancels the closing of the form.
    '-------------------------------------------------------------------

    That's right!, this seems to be the solution for my case
    (close the form without taking care of each control's validating event )

Comments have been disabled for this content.