关于textbox的限制输入(C#代码)

最近做C#winform 要求只能输入小数,所以测试了几回,用下来不错,博友觉得有用的话,就收藏一下!

 

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            string skey = TxB_A1.Text.Trim();
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)&&e.KeyChar!=46)
            {   //只能输入数字或者小数点,还有使用退格键
                e.Handled = true;
            }
            else if (e.KeyChar == 46)
            {
                //只能输入一个小数点或者小数点不能出现在首位
                if(skey.IndexOf('.')!=-1||skey.Length==0)
                    e.Handled=true;              
            }
            else if (e.KeyChar == 48)//首位输入不能为0
            {
                if (skey.Length == 0)
                    e.Handled = true;
            }      

          }

若有不当,请告知,大谢!

posted on 2012-04-05 14:37  sanglei  阅读(669)  评论(3编辑  收藏  举报

导航