您现在的位置:首页 >> 软件开发 >> 内容

C#窗体中文本框输入字符和数字限制

时间:2023-12-12 23:13:44 点击:

  核心提示://只能输入数字和小数点和退格键 private void txt_KeyPress(object sender, KeyPressEventArgs e) { if (((int)e.KeyChar...

//只能输入数字和小数点和退格键


    private void txt_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
        {
            e.Handled = true;


        }
    }


    //只能输入数字和退格键


    private void txt_KeyPress(object sender, KeyPressEventArgs e)


    {
        if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)8)


        {
            e.Handled = true;
        }
    }


    //限制输入只能为数字


    private void txt_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (Char)8)
        {
            e.Handled = true;
        }


    }


    //限制输入不能为中文和全角


    private void txt_KeyPress(object sender, KeyPressEventArgs e)
    {
        int chfrom = Convert.ToInt32("4e00", 16);    //范围(0x4e00~0x9fa5)转换成int(chfrom~chend)
        int chend = Convert.ToInt32("9fa5", 16);
        if (e.KeyChar >= (Char)chfrom && e.KeyChar <= (Char)chend)
        {
            e.Handled = true;
        }
        if (e.KeyChar >= (Char)65281 & (int)e.KeyChar <= (Char)65374)
        {
            e.Handled = true;
        }
    }


    //限制输入只能输入数字和字母,退格键
    private void txt_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z')
            || (e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == 8))
        {
            e.Handled = false;
        }
        else
        {
            e.Handled = true;
        }


    }

作者:站长 来源:网络
相关文章
  • 没有相关文章
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 陈工笔记(www.dui580.com) © 2024 版权所有 All Rights Reserved.
  • 站长:陈工 微信号:chengongbiji QQ:24498854
  • Powered by 陈工