C# 如何在3次尝试中关闭登录表单
如果用户登录3次失败,如何关闭登录表单。我应该对代码做些什么来实现它C# 如何在3次尝试中关闭登录表单,c#,C#,如果用户登录3次失败,如何关闭登录表单。我应该对代码做些什么来实现它 if (user == "Admin" && pass == "Admin") { //if user and pass are correct MessageBox.Show("Admin Access", "Access Granted", MessageBoxButtons.OK, MessageBoxIcon.Information); //will transfer
if (user == "Admin" && pass == "Admin")
{
//if user and pass are correct
MessageBox.Show("Admin Access", "Access Granted",
MessageBoxButtons.OK, MessageBoxIcon.Information);
//will transfer to the next form
frmItems frmitem = new frmItems();
frmitem.Show();
this.Hide();
}
else
{
//to make the textbox null if the pass is wrong
txtUser.Text = "";
txtPass.Text = "";
count = 0;
count++;
//if the user or pass are wrong
MessageBox.Show("Invalid User/Passwosrd", "Access Denied",
MessageBoxButtons.OK, MessageBoxIcon.Error);
/close the program if the pass/user failed 3 times
if (count == 3)
{
MessageBox.Show("This program will close", "Access Denied",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
现在,您将其设置在函数内部,因为每次变量都被设置回0
您只需要在定义count的位置将其值设置为0。希望您已经将count变量设置为全局变量。现在,您正在函数中设置它,因为每次该变量都被设置为0
您只需要在定义count的位置将其值设置为0。希望您已将count变量设置为全局变量。如果
count
定义在此范围之外,则删除行count=0
,则您的代码将正常工作没有问题,如果count
定义在此范围之外,则很乐意帮助您删除行count=0代码>,那么你的代码会很好地工作没有问题,很乐意帮助你。除了我的评论之外还有什么吗?这是同步的,如果他错过了,你没有提到让变量全局化。除了我的评论之外还有什么吗?这是同步的,如果他错过了,你没有提到让变量全局化。