如何在登录屏幕上创建登录限制?
我目前正试图在C#中设计一个atm机器,我对此很新。 我想我的登录屏幕返回到我的欢迎屏幕3尝试登录失败后,但我不知道从哪里开始,以及如何实现我的代码到我的程序来做到这一点。如何在登录屏幕上创建登录限制?
我当前的代码是我的登录屏幕如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Common;
namespace bankkk
{
public partial class FrmLogin : Form
{
public FrmLogin()
{
InitializeComponent();
}
public static OleDbConnection con = new OleDbConnection();
string dbProvider;
string dbSource;
OleDbDataAdapter da;
public static DataSet ds1 = new DataSet();
string sql;
string pin;
int rownum = 0;
bool valid = false;
private void FrmLogin_Load(object sender, EventArgs e)
{
dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;";
dbSource = "Data Source = 'd:\\bank11.accdb'";
con.ConnectionString = dbProvider + dbSource;
ds1 = new DataSet();
con.Open();
sql = " SELECT tblCustomers.* FROM tblCustomers";
da = new OleDbDataAdapter(sql, con);
rownum = da.Fill(ds1, "tblCustomers");
con.Close();
}
private void btnexit_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
this.Close();
}
//METHOD VALIDATE
private bool validate()
{
ds1 = new DataSet();
con.Open();
sql = "SELECT tblCustomers.* FROM tblCustomers WHERE ((tblCustomers.AccountNo) = '" + txtAccount.Text + "')";
da = new OleDbDataAdapter(sql, con);
rownum = da.Fill(ds1, "tblCustomers");
con.Close();
if (rownum != 1)
{
MessageBox.Show("Not a valid Account");
return false;
}
else
{
pin = ds1.Tables["tblCustomers"].Rows[0][4].ToString();
if (pin == txtPin.Text)
{
return true;
}
else
{
MessageBox.Show("INVALID PIN");
return false;
}
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
valid = validate();
if (valid == true)
{
if (txtAccount.Text == "11111111" && txtPin.Text == "9999")
{
Frmmanager Manager = new Frmmanager();
this.Close();
Manager.Show();
}
else
{
frmaccount account = new frmaccount();
this.Close();
account.Show();
{
txtAccount.Clear();
txtPin.Clear();
}
}
}
}
private void btnlogin_Click_1(object sender, EventArgs e)
{
valid = validate();
if (valid == true)
{
if (txtAccount.Text == "11111111" && txtPin.Text == "9999")
{
Frmmanager Manager = new Frmmanager();
this.Close();
Manager.Show();
}
else
{
frmaccount account = new frmaccount();
this.Close();
account.Show();
{
txtAccount.Clear();
txtPin.Clear();
}
}
}
}
}
}
你说返回到我的欢迎屏幕,所以我会假设你正在使用,而不是.ShowDialog()
的FrmLogin
显示.Show()
。这样,你只需要关闭你的旧形式。
如果你只是在做.Show()
,你可以做这样的事情:
public partial class FrmWelcome {
//in some part of your code...
var frmLogin = new FrmLogin();
//when the login form is closed, show this one.
//Depending on your application, you might want to add some boolean variable
//to the Login Form that will be true if the user authentication failed, and
//show this form again only if it is true.
frmLogin.Closed += (s, e) => this.Show();
this.Hide();
frmLogin.Show();
}
尝试下面的代码。这个想法是有一个failedAttempts
变量,每当你的验证代码失败时它会增加。当它等于3时,您只需关闭表格(参见上文)。
namespace bankkk
{
public partial class FrmLogin : Form
{
public FrmLogin()
{
InitializeComponent();
}
...
int failedAttempts = 0;
private void btnlogin_Click_1(object sender, EventArgs e)
{
valid = validate();
if (!valid) {
//Increment the number of failed attempts
failedAttempts += 1;
//If equal to 3
if (failedAttempts == 3) {
//Just close this window
this.Close();
}
}
else
{
//the same code you were using...
}
}
}
}
如果你为他做这件事,他不会学习。 :P – 2013-03-08 20:11:08
@DavidHughes我编辑了我的答案,除了代码外还添加了一些解释:) – 2013-03-08 20:16:31
谢谢Thats Great :) – 2013-03-09 17:40:23
您可以添加一个变量到您的程序并增加它以限制尝试登录的次数。要限制帐户尝试的次数,请添加一个表以将包括无效尝试在内的登录信息存储到数据库。将该表格链接到您的客户表格。当无效尝试登录到有效的客户帐户时会增加无效尝试的次数并将其写回登录表。在成功登录后,您可以将无效尝试的次数设置为0.
O.o在执行SQL语句之后,您不知道从哪里开始?您是否考虑过从头开始制作简单的登录应用程序,而不使用数据库? – 2013-03-08 20:02:37
您只需将失败的登录数存储在一个变量中,然后检查该变量。 – 2013-03-08 20:02:46
使用变量来存储失败的登录尝试的次数,在每次登录失败时增加这个值,检查按钮上的变量值是否按顺序点击 – 2013-03-08 20:03:04