コード例 #1
0
    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;
                });
            }
        }
    }
コード例 #2
0
 protected virtual void OnTextValidating(object sender, TextValidatingEventArgs e) => TextValidating?.Invoke(sender, e);
コード例 #3
0
 protected override void OnTextValidating(object sender, TextValidatingEventArgs e)
 {
     e.Cancel = !int.TryParse(e.NewText, out int i);
 }