private void txtDishName_Validating(object sender, CancelEventArgs e) { string errorMsg; if (!ValidDishName(TxtName.Text, out errorMsg)) { //cancel the event and select the text to be corrected by the user e.Cancel = true; TxtName.Select(0, TxtName.Text.Length); //Set the ErrorProvider error with the text to display this.errorProvider1.SetError(TxtName, errorMsg); } if (e.Cancel == false) { this.errorProvider1.Clear(); } }