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.
curious77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JackOfPHCommented:
Put it in the form_closing event instead...
0
curious77Author Commented:
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.
0
JackOfPHCommented:
Sorry, my interpretation is wrong... can you show us the code in the cancel button instead?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

curious77Author Commented:
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

0
omegaomegaDeveloperCommented:
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
0
curious77Author Commented:
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.
0
omegaomegaDeveloperCommented:
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
0
zen770Commented:
I have had similar problem myself, and setting the AutoValidate=EnableAllowFocusChange in Form Properties solved the problem. What was happening in my case was that while the button click didn't invoke the Validation moving the focus did. However like you it worked fine in other Forms without this change.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
omegaomegaDeveloperCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.