Link to home
Start Free TrialLog in
Avatar of curious77
curious77

asked on

Form.Close triggering validation?

I have some forms that when I do me.close() in code it triggers the validation. On others it does not. What can be causing this? Where would I set the CausesValidation for Close? I need to stop this cause when user presses the cancel button it tries to validate intead of just cancelling (closing) the form.
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

Put it in the form_closing event instead...
Avatar of curious77
curious77

ASKER

put what in the form closing?
now I have a button "Cancel" which does me.close. It works in some of my forms, but not in others. All the forms have error providers setup and does validation, but on some it seems to get triggered on me.close while others it does not.
Sorry, my interpretation is wrong... can you show us the code in the cancel button instead?
me.close
   Private Sub ButCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles ButCancel.Click
        Me.Close()
    End Sub

Open in new window

Avatar of omegaomega
Hello, curious77,

I think that perhaps you just need to set the CausesValidation property of the ButCancel button to False.
 BTW -- If you set the Form's CancelButton property to be ButCancel, you won't require the event handler.

Cheers,
Randy
omegaomega,

CausesValidation is/was set to False. Keep in mnd that it's not the Butto click that is triggering the Validation but rather the me.close.

I changed the Form Cancel to my Cancel Button and I removed the me.close but now nothing hapens when pressed. Am I suppose to do something more? If I leave the me.close then I have the same Validatoin triggering problem.
Hello, curious77,

Re: setting the Form's CancelButton property -- Please ignore everything I said.  I seem to have had an out-of-brain experience. :-(  Setting the CancelButton property only ties the button click to the <Esc> key.  You still need the Close method in the click event.

Re: the validation problem -- I don't understand this either.  I tries it on a trivial dummy application and found the same problem you are having.  And yet, I'm sure that I have done the same thing without any problem in a much more complex application.

I will try to find out why it seems to work in my "real" application but not in the "dummy" application and will get back to you if I learn anything.

Cheers,
Randy
ASKER CERTIFIED SOLUTION
Avatar of zen770
zen770

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello, zen770,

Thanks for pointing that out.  This property is new to me. (New in v2.0, apparently.)  There doesn't seem to be any useful help available on this, but I found that in my simple "dummy" application, setting it to either EnableAllowFocusChange or Disable seemed to solve the problem.  When set to EnablePreventFocusChange the problem occurs.

The forms in the "real" application I mentioned have this property set to EnablePreventFocusChange, and yet I have no problem with validation events firing when the form is closed (via the Cancel button).  This application has been converted from VB-2003, and so maybe there is something associated with the conversion that prevents the problem from happening in this case.

Cheers,
Randy