Home > Net >  The login interface read documents login validation issues, and the most handsome great god arranged
The login interface read documents login validation issues, and the most handsome great god arranged

Time:09-25

Read the file after the login account and password can log in successfully as long as it is in the listbox, ex: (Tester1, 2 indicates that great god help landing successful), the following is a program and TXT documents content:

CodePudding user response:

Should cycle is wrong, one to one

CodePudding user response:

With this, a bit better: with key/value pair, can guarantee the user name and password are one-to-one correspondence, the user name should not be case-sensitive

Public partial class Form1: Form
{
Private Dictionary UserPwds=new Dictionary (a);
Public _click ()
{
InitializeComponent();
Enclosing LoadUsers ();
}

Private void LoadUsers ()
{
Enclosing userPwds=new Dictionary (a);
String [] lines=File. ReadAllLines (" d: \ \ test. TXT ");
The foreach (string line in lines)
{
String [] userPwd=line. The Split (new char [] {', '});
If (userPwd. Length==2)
{
if (! String. IsNullOrWhiteSpace (userPwd [0])
{
Enclosing userPwds. Add (userPwd [0], userPwd [1]).
}
}
}
}

Private void buttonLogin_Click (object sender, EventArgs e)
{
String user=this. TextBoxUser. Text;
String PWD=this. TextBoxPwd. Text;
If (string. IsNullOrWhiteSpace (user))
{
MessageBox. Show (" please enter the user name ");
return;
}
Int flag=0;
The foreach (var up in userPwds)
{
If (string.Com pare said (user, up Key)==0)
{
Flag=1;//user name to
If (string.Com pare said (PWD, up. Value)==0)
{
Flag=2;//password to
}
break;
}
}
If (flag==0)
{
MessageBox. Show (" the user name does not exist ");
return;
}
If (flag==1)
{
MessageBox. Show (" password error ");
return;
}
If (flag==2)
{
MessageBox. Show (" login succeeds ");
return;
}
}

Private void buttonCancel_Click (object sender, EventArgs e)
{
Enclosing the Close ();
}
}
  •  Tags:  
  • C #
  • Related