C# 取消/清除按钮事件发回并尝试验证字段

C# 取消/清除按钮事件发回并尝试验证字段,c#,C#,这让我快发疯了 我在Windows窗体控件上有一段代码,该代码确保窗体清除并将焦点放回第一个控件(电话号码)。问题是我使用的是休假事件句柄,此句柄包含验证代码,以便在使用离开控件时验证手机 当我点击重置或退出表单时,它不仅会清除表单,还会将焦点发送回电话字段,从而使控件(文本框)生效 我需要将焦点放在手机控件上,在验证焦点时,有什么方法可以防止这种行为 private void txtPhone_Leave(object sender, EventArgs e) {

这让我快发疯了

我在Windows窗体控件上有一段代码,该代码确保窗体清除并将焦点放回第一个控件(电话号码)。问题是我使用的是休假事件句柄,此句柄包含验证代码,以便在使用离开控件时验证手机

当我点击重置或退出表单时,它不仅会清除表单,还会将焦点发送回电话字段,从而使控件(文本框)生效

我需要将焦点放在手机控件上,在验证焦点时,有什么方法可以防止这种行为

private void txtPhone_Leave(object sender, EventArgs e)
        {
             Int64 ConvertPhone;
            if (txtPhone.Text.Trim().Length != 10)
            {
                lblPhoneError.Visible = true;
                lblErrorIndicator.Visible = true;
                lblErrorIndicator.Text = "*Valid 10 digit phone number required";
            }

            else if (Int64.TryParse(txtPhone.Text, out ConvertPhone))
            {
                lblPhoneError.Visible = false;
                lblErrorIndicator.Visible = false;
                txtPhone.MaxLength = 10;
                txtPhone.Text = txtPhone.Text.Substring(0, 3) + "." + txtPhone.Text.Substring(3, 3) + "." + txtPhone.Text.Substring(6, 4);
           }

        }

 private void btnClear_Click(object sender, EventArgs e)
    {
        txtPhone.Clear();
        txtPhone.Focus();
    }


    private void txtPhone_Enter(object sender, EventArgs e)
    {
        txtPhone.Text = txtPhone.Text.Replace(".", "");
    }
谢谢大家

if(txtPhone.Text.Trim().Length!=10) {

            if (txtPhone.Text != "")
            {
                lblErrorIndicator.Visible = true;
                lblErrorIndicator.Text = "*Valid 10 digit phone number required";
            }
        }
私有void btnClear\u单击(对象发送者,事件参数e) { txtPhone.Clear(); lblErrorIndicator.Text=“”; 焦点(); }


我能理解你的问题,但最后告诉我你想做什么?

你能发布代码吗?我也在使用Enter句柄(事件)对于电话格式,但我希望在此保持简短。最后,我只需要将焦点返回到电话文本框,但不触发电话文本框中的休假事件的验证。您的解决方案解决了此问题,但现在我无法将电话号码的格式设置为原始代码中的格式:567.876.1234。