C#Winform文本框输入后清空,再次输入就有问题,不清空手工清空就没问题,为什么?

我的步骤是
1、随便输会提示格式错误
2、然后输入正确的格式,如果有textBox1.Text = "",他就会报第二张图的错误,如果注释掉,手工全选清除再输入就正确

img

这个工单为空是接口给的错误信息,我直接那来调用的
但是如果我注释掉textBox1.Text = "";然后手工全选删除,再输入后就能获取到工单了!请问为什么

img


        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string pattern = @"\d*-\d*-\d*";
                Regex regex = new Regex(pattern);
                if (!regex.IsMatch(this.textBox1.Text.Trim()))
                {
                    MessageBox.Show("格式错误!");
                    judgement = 1;
                }
                else
                {
                    if (judgement == 1)
                    {
                        judgement = 0;
                        string sendjson = "work_order_no=" + textBox1.Text;
                        requestorder("http://xxx", sendjson, "POST");
                    }
                }
                textBox1.Text = "";
            }
        }

改成这样试一下是什么效果

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            string val = this.textBox1.Text.Trim();
            if (e.KeyChar == 13)
            {
                string pattern = @"\d*-\d*-\d*";
                Regex regex = new Regex(pattern);
                if (!regex.IsMatch(val))
                {
                    MessageBox.Show("格式错误!");
                    judgement = 1;
                }
                else
                {
                    if (judgement == 1)
                    {
                        judgement = 0;
                        string sendjson = "work_order_no=" + val;
                        requestorder("http://xxx", sendjson, "POST");
                    }
                }
                textBox1.Text = "";
            }
        }

第二次输入时你得整体变量judgement需要重新初始化赋值一下

我测试两种方式得到的sendjson是一样的。
尝试过textBox1.Clear()吗?