zoukankan      html  css  js  c++  java
  • C# TextBox文本框只能输入数字、小数点(最大到2位)、退格键、负号

    要实现TextBox文本框输入限制 ,先要为TextBox添加KeyPress事件。

    代码如下:

            //数字、小数点(最大到2位)、退格键、负号
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-'))
                {
                    e.Handled = true;
                }
                if (e.KeyChar == (char)('-'))
                {
                    if ((sender as TextBox).Text != "")
                    {
                        e.Handled = true;
                    }
                }
                //第1位是负号时候、第2位小数点不可
                if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
                {
                    e.Handled = true;
                }
                //负号只能1次
                if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0))
                    e.Handled = true;
                //第1位小数点不可
                if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
                {
                    e.Handled = true;
                }
                //小数点只能1次
                if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf('.') != -1)
                {
                    e.Handled = true;
                }
                //小数点(最大到2位)   
                if (e.KeyChar != '' && (((TextBox)sender).SelectionStart) > (((TextBox)sender).Text.LastIndexOf('.')) + 2 && ((TextBox)sender).Text.IndexOf(".") >= 0)
                    e.Handled = true;
                //光标在小数点右侧时候判断  
                if (e.KeyChar != '' && ((TextBox)sender).SelectionStart >= (((TextBox)sender).Text.LastIndexOf('.')) && ((TextBox)sender).Text.IndexOf(".") >= 0)
                { 
                    if ((((TextBox)sender).SelectionStart) == (((TextBox)sender).Text.LastIndexOf('.')) + 1)
                    {
                        if ((((TextBox)sender).Text.Length).ToString() == (((TextBox)sender).Text.IndexOf(".") + 3).ToString())
                            e.Handled = true;
                    }  
                    if ((((TextBox)sender).SelectionStart) == (((TextBox)sender).Text.LastIndexOf('.')) + 2)
                    {
                        if ((((TextBox)sender).Text.Length - 3).ToString() == ((TextBox)sender).Text.IndexOf(".").ToString()) e.Handled = true;
                    }
                }
                //第1位是0,第2位必须是小数点
                if (e.KeyChar != (char)('.') && e.KeyChar != 8 && ((TextBox)sender).Text == "0")
                {
                    e.Handled = true;
                }
            }

    转 : https://blog.csdn.net/qq_42675313/article/details/81111388

  • 相关阅读:
    汇编笔记
    C++知识点复习
    flask 初步
    flask and postgre on heroku
    google zxing二维码库 初始
    flasklogin解读
    flasksqlalchemy 关系(一对多)
    flask的信号
    flask 范例学习
    github 操作纪录
  • 原文地址:https://www.cnblogs.com/fps2tao/p/14777295.html
Copyright © 2011-2022 走看看