其他分享
首页 > 其他分享> > WinForm的TextBox限制只能输入数字

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;
            }
        }
    }

 

标签:RBUTTONDOWN,WndProc,int,WM,private,CHAR,WinForm,输入,TextBox
来源: https://www.cnblogs.com/qianqianhongru/p/11078354.html