private void OnTextValidating(object sender, TextValidatingEventArgs e) { var styles = RadioButtonHexadecimal.Checked ? NumberStyles.HexNumber : NumberStyles.Integer; if (_value is long) { e.Cancel = !long.TryParse(e.NewText, styles, null, out long l); } else { e.Cancel = !int.TryParse(e.NewText, styles, null, out int i); } }
protected override void WndProc(ref Message m) { base.WndProc(ref m); if (_dontProcessMessages) { return; } const int WM_KEYDOWN = 0x100; const int WM_ENTERIDLE = 0x121; const int VK_DELETE = 0x2e; bool delete = m.Msg == WM_KEYDOWN && (int)m.WParam == VK_DELETE; if ((m.Msg == WM_KEYDOWN && !delete) || m.Msg == WM_ENTERIDLE) { DontProcessMessage(() => { _validText = Text; _selectionStart = SelectionStart; _selectionEnd = SelectionLength; }); } const int WM_CHAR = 0x102; const int WM_PASTE = 0x302; if (m.Msg == WM_CHAR || m.Msg == WM_PASTE || delete) { string newText = null; DontProcessMessage(() => { newText = Text; }); var e = new TextValidatingEventArgs(newText); OnTextValidating(this, e); if (e.Cancel) { DontProcessMessage(() => { Text = _validText; SelectionStart = _selectionStart; SelectionLength = _selectionEnd; }); } } }
protected virtual void OnTextValidating(object sender, TextValidatingEventArgs e) => TextValidating?.Invoke(sender, e);