WinForm的TextBox限制只能输入数字并且屏蔽默认右键菜单

基于Window消息实现

class TextBoxExt:TextBox
    {
        private const int WM_RBUTTONDOWN = 0x0204;
        private const int WM_CHAR = 0x0102;
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_RBUTTONDOWN:
                    return;//屏蔽默认右键菜单
                    break;
                case WM_CHAR:
                    int n = (int)m.WParam;
                    if (n==8)//允许输入退格键
                    {
                        base.WndProc(ref m);
                    }
                    if (n>=47 && n<=57)//允许输入0-9
                    {
                        base.WndProc(ref m);

                    }
                    else
                    {
                        return;
                    }
                    break;
                default:
                    base.WndProc(ref m);
                    break;
            }
        }
    }

 

posted on 2019-06-24 17:34  浅浅鸿儒  阅读(169)  评论(0编辑  收藏  举报

导航