Here are the steps:
Step 1. Create a Windows form application.

Step 2: Select the toolbar of the "ErrorProvider" form.


Step 3: Select the text box and go to its properties.

In the properties, select "Events" and in the focus, double-click "Check". Now we have a method for checking a text field.
private void textBoxName_Validating(object sender, CancelEventArgs e) { if (string.IsNullOrWhiteSpace(textBoxName.Text)) { e.Cancel = true; textBoxName.Focus(); errorProviderApp.SetError(textBoxName, "Name should not be left blank!"); } else { e.Cancel = false; errorProviderApp.SetError(textBoxName, ""); } }
Step 4: Now the check should be started when you press Enter. Add the following code to the Enter key method.
private void buttonEnter_Click(object sender, EventArgs e) { if (ValidateChildren(ValidationConstraints.Enabled)) { MessageBox.Show(textBoxName.Text, "Demo App - Message!"); } }
Also make sure that the CauseValidation Enter button property is set to true.
Now our window form is ready for inspection.
https://www.c-sharpcorner.com/blogs/how-to-use-validation-in-windows-form-application
source share