Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何编写代码以防止正则表达式显示此错误消息,并通过单击“关闭”按钮随时关闭该表单?_C#_Regex_Windows_Forms_Visual Studio - Fatal编程技术网

C# 如何编写代码以防止正则表达式显示此错误消息,并通过单击“关闭”按钮随时关闭该表单?

C# 如何编写代码以防止正则表达式显示此错误消息,并通过单击“关闭”按钮随时关闭该表单?,c#,regex,windows,forms,visual-studio,C#,Regex,Windows,Forms,Visual Studio,我为一家书店创建了windows窗体应用程序。它有一个登录窗体,如果用户忘记了密码,他可以通过单击“忘记密码”按钮更改密码。在“重置密码”表单中,我提供了正则表达式来验证用户名、新密码、确认密码文本框。此外,如果用户不希望重置其密码,它还有一个关闭按钮来关闭“重置密码”表单然后我的问题是,当用户单击“关闭”按钮时,它无法关闭表单,并显示我为文本框的正则表达式创建的错误消息。如何编写代码以防止正则表达式显示此错误消息,并通过单击“关闭”按钮随时关闭该表单? 这是该形式的代码 using S

我为一家书店创建了windows窗体应用程序。它有一个登录窗体,如果用户忘记了密码,他可以通过单击“忘记密码”按钮更改密码。在“重置密码”表单中,我提供了正则表达式来验证用户名、新密码、确认密码文本框。此外,如果用户不希望重置其密码,它还有一个关闭按钮来关闭“重置密码”表单然后我的问题是,当用户单击“关闭”按钮时,它无法关闭表单,并显示我为文本框的正则表达式创建的错误消息。如何编写代码以防止正则表达式显示此错误消息,并通过单击“关闭”按钮随时关闭该表单?

这是该形式的代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using MySql.Data.MySqlClient;
    using System.Text.RegularExpressions;
    using BookShopApp.Connection;

    namespace BookShopApp.forms
    {
        public partial class Form_forgetPwd : Form
    [enter image description here][1]{
    MySqlConnection conn = null;
    string userName;
    string password;
    string confirm_Password;

    public Form_forgetPwd()
    {
        InitializeComponent();
        ConnectDB cndb = new ConnectDB();
        conn = cndb.ConnectDatabase();
    }

    private void btnclose_Click(object sender, EventArgs e)
    {
     
        this.Close();
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        userName = txtUsername.Text;
        password = txtPwd.Text;
        confirm_Password = txtConPwd.Text;

        
            try
            {

                conn.Open();
                string cnestring = "SELECT User_Name FROM wisdom.user_detail WHERE User_Name='" + userName + "'";
                string UP = " UPDATE wisdom.user_detail SET Password = '"+password+"' WHERE User_Name = '"+userName+"'";
                MySqlCommand cmd = new MySqlCommand(cnestring,conn);
                MySqlCommand cmdU = new MySqlCommand(UP, conn);
                cmdU.ExecuteNonQuery();
                MySqlDataReader red = cmd.ExecuteReader();
                if (red.Read())
                {
                Login log = new Login();
                    log.Hide();
                    this.Hide();
                    MessageBox.Show("Password Successfully Changed !","Success",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Invalid Login please check username and password");
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
    

    private void btnClear_Click(object sender, EventArgs e)
    {
        txtUsername.Clear();
        txtPwd.Clear();
        txtConPwd.Clear();
    }

    private void txtUsername_Leave(object sender, EventArgs e)
    {
        userName = txtUsername.Text;

        try
        {

            conn.Open();
            string cnestring = "SELECT User_Name FROM wisdom.user_detail WHERE User_Name='" + userName + "'";
            MySqlCommand cmd = new MySqlCommand(cnestring, conn);
            MySqlDataReader red = cmd.ExecuteReader();
            if (red.Read())
            {
                //MessageBox.Show(" success");
            }
            else
            {
                MessageBox.Show("Invalid  username","Erorr",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


    
}

    private void txtPwd_TextChanged(object sender, EventArgs e)
    {
        lblPwdInfo.Hide();
    }

    private void txtPwd_Enter(object sender, EventArgs e)
    {
        lblPwdInfo.Show();
    }

    private void txtPwd_Leave(object sender, EventArgs e)
    {
        password = txtPwd.Text;
        try
        {

            string regExpPwd = "^([a-zA-Z0-9]{8,15})$";
            Regex PwdRE = new Regex(regExpPwd);
            
            if (PwdRE.IsMatch(password))
            {
                password = password;
                //MessageBox.Show(password);
            }
            else
            {
                MessageBox.Show("Please Follow Password Hint & Try Again !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPwd.Focus();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void txtConPwd_Leave(object sender, EventArgs e)
    {
        if(txtPwd.Text == txtConPwd.Text)
        {
            btnSave.Focus();
        }
        else
        {
            MessageBox.Show("Password & Confirm Password is not Matched !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}   

}根据我的测试,我重现了你的问题。我建议您可以在“离开”事件的文本框中使用button.Focused属性

您可以按如下方式修改代码:

 private void txtUserName_Leave(object sender, EventArgs e)
        {
            if (btnClose.Focused)
            {
                return;
            }
            else
            {

                userName = txtUserName.Text;

                try
                {

                      conn.Open();
                      string cnestring = "SELECT User_Name FROM wisdom.user_detail WHERE User_Name='" + userName + "'";
                      MySqlCommand cmd = new MySqlCommand(cnestring, conn);
                      MySqlDataReader red = cmd.ExecuteReader();
                    if (red.Read())
                    {
                        MessageBox.Show(" success");
                    }
                    else
                    {
                        MessageBox.Show("Invalid  username", "Erorr", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            
        }

        private void txtPWD_Leave(object sender, EventArgs e)
        {
            if (btnClose.Focused)
            {
                return;
            }
            else
            {
                password = txtPWD.Text;
                try
                {

                    string regExpPwd = "^([a-zA-Z0-9]{8,15})$";
                    Regex PwdRE = new Regex(regExpPwd);

                    if (PwdRE.IsMatch(password))
                    {
                        password = txtPWD.Text;
                        //MessageBox.Show(password);
                    }
                    else
                    {
                        MessageBox.Show("Please Follow Password Hint & Try Again !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtPWD.Focus();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            

        }

        private void txtConPWD_Leave(object sender, EventArgs e)
        {
            if (btnClose.Focused)
            {
                return;
            }
            else
            {
                if (txtPWD.Text == txtConPWD.Text)
                {
                    btnSave.Focus();
                }
                else
                {
                    MessageBox.Show("Password & Confirm Password is not Matched !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }


private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
结果:

我建议定义标志,这样您就可以知道表单何时关闭。为此,我将定义:

private bool _isClosing;
然后在constructor中,将其设置为false:

public Form_forgetPwd()
{
    InitializeComponent();
    ConnectDB cndb = new ConnectDB();
    conn = cndb.ConnectDatabase();
    _isClosing = false;
}
关闭时,将其设置为true:

private void btnclose_Click(object sender, EventArgs e)
{
    _isClosing = true;
    this.Close();
}
根据该值,您可以决定是否验证密码:

private void txtPwd_Leave(object sender, EventArgs e)
{
    // if closing, ignore password validation
    if(_isClosing) return;
    // rest of the method
}

您应该在文本框上使用regex check-on-textbox验证方法,而不是在离开时method@TharakaAmarasinghe,有更新吗?如果您的问题已解决,您可以单击'✔' 将适当的回答标记为答案。