如何编码以防止此错误消息来自正则表达式并通过单击关闭按钮随时关闭该表单?
Posted
技术标签:
【中文标题】如何编码以防止此错误消息来自正则表达式并通过单击关闭按钮随时关闭该表单?【英文标题】:how can I code to prevent this error message from regular expression and close that form anytime by clicking close button? 【发布时间】:2021-05-30 02:49:03 【问题描述】:我为一家书店创建了 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);
【问题讨论】:
您应该对文本框验证方法进行正则表达式检查,而不是在离开方法时检查 @Tharaka Amarasinghe,有更新吗?如果您的问题已经解决,您可以点击“✔”将相应的回复标记为答案。 【参考方案1】:根据我的测试,我重现了您的问题。我建议你可以在 textbox_Leave 事件中使用 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();
结果:
【讨论】:
【参考方案2】:我建议定义标志,这样您就可以知道表单何时关闭。为此,我将定义:
private bool _isClosing;
然后在构造函数中,将其设置为 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
【讨论】:
以上是关于如何编码以防止此错误消息来自正则表达式并通过单击关闭按钮随时关闭该表单?的主要内容,如果未能解决你的问题,请参考以下文章
防止在正则表达式上回溯以查找非注释行(不以缩进的“#”开头)
正则表达式检测基本 SQL 注入,但不能作为防止 SQL 注入的手段