protected override void OnKeyPress(KeyPressEventArgs e) { if (!DisallowedCharacters.IsEmpty() && DisallowedCharacters.Contains(e.KeyChar)) { e.Handled = true; } base.OnKeyPress(e); }
public void Sanitize(Value value) { value.FieldName = Name ?? string.Empty; string sanitizedValue = TrimField ? value.OriginalValue.Trim() : value.OriginalValue; //If it wasn't in the original file, handle it in advance if (value.Missing || sanitizedValue == string.Empty) { if (!AllowedBlank) { value.ErrorMsg = ValueIfBlank; } return; } //Removes any disallowed characters and retains only allowed characters var chars = sanitizedValue.ToCharArray(); if (AllowedCharacters.Any()) { chars = chars.Where(c => AllowedCharacters.Any(a => a == c)).ToArray(); } if (DisallowedCharacters.Any()) { chars = chars.Where(c => !DisallowedCharacters.Any(d => d == c)).ToArray(); } //validate the length if (chars.Length > MaxLength || chars.Length < MinLength) { value.SanitizedValue = new string(chars); value.ErrorMsg = ValueIfWrongLength; return; } //run regex sanitizedValue = new string(chars); if (RegEx != string.Empty && !Regex.IsMatch(sanitizedValue, RegEx)) { value.ErrorMsg = "Value failed regex check on value."; return; } //run any custom checks value.SanitizedValue = RemoveDoubleSpaces(sanitizedValue); //remove any double spaces CustomChecks.ForEach(c => c.Execute(value)); //If this there are a fixed number of options, check for them if ((AllowedValues?.Count() ?? 0) != 0 && !AllowedValues.Any(v => v == value.SanitizedValue)) { value.ErrorMsg = ValueIfNotInAllowedOptions; } }
protected override void OnTextChanged(EventArgs e) { string oldBackingText = _backingText; if (!DisallowedCharacters.IsEmpty()) { string newText = Text; foreach (char c in DisallowedCharacters) { newText = newText.Replace(c.ToString(), ""); } // Prevents control-key combinations (Ctrl+A for example) from breaking, since they also fire // this event even though the text doesn't actually end up changing in that case. if (newText != Text) { int oldCaretPosition = SelectionStart; int oldTextLength = Text.Length; Text = newText; int newCaretPosition = oldCaretPosition - (oldTextLength - newText.Length); Select(newCaretPosition < 0 ? 0 : newCaretPosition, 0); } } _backingText = Text; // Prevents non-text key combinations from firing the TextChanged event. // How in the hell does "text changed" mean "key pressed but literally no text changed at all". // Microsoft... // 2021-03-09: // I guess I finally figured out why they did this. It's probably so that you can select a character, // overwrite it with the same one, and now your text is the same but you can still run things that // react to "text entry" (like I'm having to do with my search drop-down). if (!StrictTextChangedEvent || oldBackingText != Text) { base.OnTextChanged(e); } }