zoukankan      html  css  js  c++  java
  • 只能输入数字的TextBox

    原代码:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                
    if(!char.IsDigit(e.KeyChar)) e.Handled = true;
                
    //'\b'是退格键值
                if(e.KeyChar=='\b'||e.KeyChar=='.') e.Handled = false;
            }

    以上代码是无法限制全角数字输入的.而在项目中全角数字是不能算真正的数字,因为在参加计算时全角可能会出错.
    修改的代码:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                
    //全角占一个汉字,半角点半个汉字,所以在字节上是不同的
                
    //全角数字"KeyChar"=2,半解数字"KeyChar"=1
                byte[] array = System.Text.Encoding.Default.GetBytes(e.KeyChar.ToString());
                
    //array.LongLength,而不是array.Length
                if (!char.IsDigit(e.KeyChar)|| array.LongLength==2) e.Handled = true;
                
    //'\b'是退格键值
                if (e.KeyChar == '\b' || e.KeyChar == '.') e.Handled = false;
                 
            }
  • 相关阅读:
    5 Python3 函数进阶&迭代器与生成器
    2 python第三章文件操作
    4 python内置函数
    python内置函数 eval()、exec()以及complie()函数
    0 字符与字节的区别
    python enumerate() 函数
    1 python 文件处理
    python 之编写登陆接口
    python 之九九乘法表
    第一模块第二章-数据类型整理
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/2850617.html
Copyright © 2011-2022 走看看