c# 文本框只允许输入数字的方法

来源:互联网 发布:java properties 编辑:程序博客网 时间:2024/05/16 08:28


      此方法简单快捷, 可以借鉴,利用 KeyPressEventArgs 的Handled属性,确定文本框是否接受输入。


       private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            const int BACKSPACE = 8;
            const int ZERO = 48;
            const int NINE = 57;

            int keyvalue = e.KeyChar;

            if ((keyvalue == BACKSPACE) || ((keyvalue >= ZERO) && (keyvalue <= NINE))) return;
            // Allow nothing else
           e.Handled = true;

        }

0 0
原创粉丝点击