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

 

上一篇:C#父子ID的表数据转换成Tree型Json数据


下一篇:动态树之link-cut tree