其他分享
首页 > 其他分享> > Winform 限制文本框输入整数为0-50

Winform 限制文本框输入整数为0-50

作者:互联网

   private void textOsDlp_KeyPress(object sender, KeyPressEventArgs e)  //文本框按键事件
      {
            this.lberror.Visible = false;  //新建label信息提示
            e.Handled = true;
            if (e.KeyChar == (char)8)
            {
                e.Handled = false;
                return;
            }

            if ((e.KeyChar >= '0' && e.KeyChar <= '9'))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
                return;
            }

            if (!string.IsNullOrEmpty(this.textOsDlp.Text))
            {
                int number = e.KeyChar - '0';
                double total = int.Parse(this.textOsDlp.Text) * 10 + number;

                if ((total > 50 || total < 0))
                {
                    this.lberror.Visible = true;   
                    //e.Handled = true;
                    if ((int.Parse(this.textOsDlp.Text) > 50 || int.Parse(this.textOsDlp.Text) < 0))
                    {
                        //this.lberror.Visible = true;
                        e.Handled = true;
                    }
                    else
                    {
                        e.Handled = false;

                    }
                }
            }

            //this.lberror.Visible = false;
            //e.Handled = true;

            //e.Handled = true;
            //if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == (char)8)
            //{
            //    e.Handled = false;
            //}
            //if (!string.IsNullOrEmpty(this.textOsDlp.Text) && (int.Parse(this.textOsDlp.Text) > 50 || int.Parse(this.textOsDlp.Text) < 0))
            //{
            //    this.lberror.Visible = true;
            //}
        }

 

标签:false,Handled,KeyChar,lberror,50,文本框,Visible,true,Winform
来源: https://www.cnblogs.com/nishang/p/15601429.html