• <转载>只能输入数字的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;

    }

  • 相关阅读:
    POJ3164 Command Network
    UVa11401 Triangle Counting
    UVa11174 Stand in a Line
    UVa11806 Cheerleaders
    Uva11538 Chess Queen
    Bzoj3130 [Sdoi2013]费用流
    Bzoj3262 陌上花开
    模拟25A 题解
    模拟24 题解
    模拟23 题解
  • 原文地址:https://www.cnblogs.com/ChangTan/p/2096823.html
Copyright © 2020-2023  润新知