protected override void OnKeyUp(KeyEventArgs e) { if (this.Validate) { while (this.Text.Length >= 1 && !this.ValidateControl()) { this.Text = this.Text.Remove(this.Text.Length - 1); this.SelectionStart = this.Text.Length; } if (this.ValidateControl()) { CurrentErrorProvider.SetError(this, ""); } } base.OnKeyUp(e); }
public bool ValidateControl() { MessageList.Clear(); string TextToValidate; Regex expression; try { TextToValidate = this.Text; switch (_Validation) { case SynapseTextBoxValidation.Text: case SynapseTextBoxValidation.Number: expression = new Regex(RegexValidations[(int)_Validation]); //CDC525 14-03-2017 impossible to correct it via control properties => must accept ? // expression = new Regex(@"[\w+]|_"); doesn't work(((((( break; case SynapseTextBoxValidation.Decimal: expression = new Regex(RegexValidations[(int)_Validation].Replace("[XX]", _NumberOfDecimal.ToString()), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled); break; default: expression = new Regex(_RegularExpression); break; } } catch { return(false); } bool empty = TextToValidate.Length == 0; bool match = expression.IsMatch(TextToValidate); //CDC525 14-03-2017 impossible to correct it via control properties => must accept ? if (TextToValidate.IndexOf("?") > -1) { match = true; } bool toolong = TextToValidate.Length > _MaxLength; bool ControlOnError = false; match = match & (TextToValidate.Length >= _MinLength); if (empty && _Mandatory) { CurrentErrorProvider.SetError(this, _MandatoryErrorMessage); MessageList.Add(_MandatoryErrorMessage); ControlOnError = true; } if (toolong) { CurrentErrorProvider.SetError(this, _TooLongErrorMessage); MessageList.Add(_TooLongErrorMessage); ControlOnError = true; } if (!empty && !match) { CurrentErrorProvider.SetError(this, _NotMatchErrorMessage); MessageList.Add(_NotMatchErrorMessage); ControlOnError = true; } if (ControlOnError) { this.BackColor = Color.Tomato; } else { this.BackColor = Color.White; } return(!ControlOnError); }