Rod
Rod

Reputation: 15457

errorprovider won't let me exit the form until I clear the error

Why is it when I first come to my window which is a form with a user control and combo box set to blank. I can click Cancel without any problem and window closes. However, if I touch the combo box and then leave it blank and I try to click Cancel the cancel event doesn't fire?

Upvotes: 2

Views: 3053

Answers (4)

DrMarbuse
DrMarbuse

Reputation: 870

Rod's answer leads to the solution. If the forms (container of the error-provider) AutoValidate-property is set to EnableAllowFoucsChange, button events are handled and in the Click-Event now it is possible to check if the form has invalid children:

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    If Not ValidateChildren() Then Exit Sub
End sub

Upvotes: 0

Neil Watson
Neil Watson

Reputation: 2831

All you actually need is

 e.Cancel = False;

in your FormClosing event.

Upvotes: 5

Kshitij Thube
Kshitij Thube

Reputation: 21

I had the same issues, but then I solved it by

  • Setting the CausesValidation property of the button to False.
  • Re-setting all the controls on the form.
  • Closing the Form.

    while (Controls.Count > 0)
          {
              Controls[0].Dispose();
          }
          this.Close();
    

I added this code to the Button_Click() event as well as the From_Closing event. Regards, Kshitij Thube

Upvotes: 0

Rod
Rod

Reputation: 15457

I had to use container.AutoValidate = .EnableAllowFocusChange

Upvotes: 2

Related Questions