

CodePudding user response:
Should cycle is wrong, one to oneCodePudding 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
Public _click ()
{
InitializeComponent();
Enclosing LoadUsers ();
}
Private void LoadUsers ()
{
Enclosing userPwds=new Dictionary
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 ();
}
}