Search code examples
c#winformsvalidationevent-handlingtextbox

WinForms Validating event not fired?


I have multiple TextBoxes and whenever they lose focus / another box receives focus, I want their content to be validated. For example if I jump to the next textbox using tab or mouse.

Each of the textboxes has an event handler.

 this.textBox.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating);

textBox.CausesValidation is true. So it should cause validation when it receives focus.

Yet none of the TextBoxes fires its validating event. The event handler is not called.

What am I missing? I have no issues handling other events.


Solution

  • The AutoValidate property of the parent UserControl was set to Systems.Windows.Forms.AutoValidate.Disable

    This disables automatic validation on focus change.

    https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.autovalidate?view=windowsdesktop-7.0#system-windows-forms-form-autovalidate

    https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.autovalidate?view=windowsdesktop-7.0