c#中设置像数量,价格,金额等的textbox的限制条件,用户只能输入数字或小数(小数及负号只能输一次)

       //判断按键是不是要输入的类型。
         if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar !=46 && (int)e.KeyChar !=45 )
            e.Handled = true;

         //小数点的处理。
         if ((int)e.KeyChar == 46){                           //小数点
            if (txtAdjustment.Text.Length <= 0)
                e.Handled = true;   //小数点不能在第一位

            else{                                             //处理不规则的小数点
                char strAdjustment = Convert.ToChar(txtAdjustment.Text.Substring(0,1));
                if((int)strAdjustment == 45 && txtAdjustment.Text.Length == 1)  //小数点不能在负号后面
                    e.Handled = true;  
                else{
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(txtAdjustment.Text, out oldf);
                    b2 = float.TryParse(txtAdjustment.Text + e.KeyChar.ToString(), out f);
                    if (b2 == false){
                        if (b1 == true)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }
        }else if((int)e.KeyChar == 45){                          //负号
           if (txtAdjustment.Text.Length >= 1)
                e.Handled = true;   //负号只能在第一位
        }


参考文档:http://blog.sina.com.cn/s/blog_a9091a33010162iv.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值