Winform 限制文本框输入整数为0-50(Enter an integer of 0-50 in the WinForm limit text box)

   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;
            //}
        }
————————
   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;
            //}
        }