小伟

小伟

 

Winform中限制输入两位小数

 

 

        public static bool txtboxliangweixiaoshu(KeyPressEventArgs e,string tbtext)
        {
            if (tbtext.Trim().Length > 0 && tbtext.Trim().IndexOf(".") > 0)
            {
                if (tbtext.Trim().IndexOf(".") < tbtext.Trim().Length - 1)
                {
                    if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
                    {
                        return true;
                    }
                    else
                    {
                        if ((tbtext.Trim().Length - tbtext.Trim().IndexOf(".")) > 2)
                        {

                            if (e.KeyChar == 8)
                            {
                                return false;
                            }
                            else
                            {
                                return true;
                            }

                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    if (e.KeyChar == 8 || e.KeyChar != 46)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
            }
            else
            {
                if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != 46)
                {
            
                        return true;
                   
                }
                else
                {
                   if (e.KeyChar == 46 && tbtext.Trim().Length == 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                 
                }
            }
        }

 

 

 

        private void tb_shishoubaofei_KeyPress(object sender, KeyPressEventArgs e)
        {

            e.Handled = UIContrl.txtboxliangweixiaoshu(e, this.tb_shishoubaofei.Text);
        }

 

posted on 2010-10-18 15:53  旋风  阅读(398)  评论(0)    收藏  举报

导航