2017-12-01 5 views
1

과 일치하는 항목이 없으면 MessageBox에 대한 피드백을 제공합니다. 현재 로그인 시스템을 만들고 있으며 사용자 이름이나 비밀번호가 잘못되었을 때 피드백을 보내려고합니다.텍스트 파일 C#

Ryan:password 
Username:password 

내가 Ryan를 입력하고 password, 잘 작동하고 다음 양식으로 나를 제공 : 여기

나는 세부 사항을 읽고 있어요있는 텍스트 파일의 내용이다. 그러나 Usernamepassword을 입력하면 먼저 'Username Incorrect'메시지 상자가 나타나고 그 메시지 상자를 닫으면 다음 양식으로 넘어갑니다.

두 번째 줄에 세부 정보를 입력하더라도 사용자 이름이 잘못된 MessageBox를 먼저 표시하지 않고 직접 다음 양식으로 가져오고 싶습니다. 앞으로는 텍스트 파일에 더 많은 행이있을 것입니다.

도움을 주시면 감사하겠습니다. 당신의 senario에서

private void button1_Click(object sender, EventArgs e) 
{ 
    string[] userdetails = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "UserDetails.txt"); 
    foreach (string user in userdetails) 
    { 
     string[] splitDetails = user.Split(':'); 
     Login.username = splitDetails[0]; 
     Login.password = splitDetails[1]; 

     label1.Text = Login.username; 
     label2.Text = Login.password; 

     if ((txtUsername.Text == Login.username) && (txtPassword.Text == Login.password)) 
     { 
      MessageBox.Show("Welcome " + Login.username); 
      this.Hide(); 
      frmMainMenu menu = new frmMainMenu(); 
      menu.Show(); 
      break; 
     } 
     else 
     { 
      if ((txtUsername.Text == Login.username) && (txtPassword.Text != Login.password)) 
      { 
       MessageBox.Show("Password incorrect"); 
       break; 
      } 
      if(txtUsername.Text != Login.username) 
      { 
       MessageBox.Show("Username incorrect"); 
      } 
     } 
    } 
} 

답변

1

처럼 코딩해야합니다.

자신에게 물어보십시오. 자격 증명 목록을 언제보아야합니까?

사용자 이름이 고유하다고 가정하면 루프를 망칠 수있는 상황이 하나 뿐이며 "사용자 이름을 찾았습니다."

목록에 입력 된 사용자 이름을 찾으면 루프가 중단되어야한다는 것을 알게됩니다. 그런 다음 암호가 올바른지 여부 만 확인하면됩니다.

암호가 맞으면 새 창을 열고 return 함수를 실행하면 작업이 완료되었습니다.

그리고 루프가 끝나면 사용자 이름을 찾았는지 여부에 따라 MessageBox를 넣습니다.

private void button1_Click(object sender, EventArgs e) 
{ 
    string[] userdetails = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "UserDetails.txt"); 
    bool usernameFound = false; 
    foreach (string user in userdetails) 
    { 
     string[] splitDetails = user.Split(':'); 
     Login.username = splitDetails[0]; 
     Login.password = splitDetails[1]; 

     label1.Text = Login.username; 
     label2.Text = Login.password; 

     if (txtUsername.Text == Login.username) 
     { 
      if (txtPassword.Text == Login.Password) 
      { 
       MessageBox.Show("Welcome " + Login.username); 
       this.Hide(); 
       frmMainMenu menu = new frmMainMenu(); 
       menu.Show(); 
       return; // we're done here, so return instead of break 
      } 
      usernameFound = true; 
      break; // we're not gonna find this username again, so might as well quit the loop 
     } 
    } 

    //we only get there if the credentials were incorrect 
    //so we check if the username was found, if yes, the 
    //password was incorrect, if not, the username was   
    string message = String.Empty; 
    if (usernameFound) 
     message = "Password"; 
    else 
     message = "Username"; 
    message += " incorrect"; 
    MessageBox.Show(message); 

    //or shorten the above 7 lines with a ternary operator 
    //MessageBox.Show((usernameFound ? "Password" : "Username") + " incorrect"); 
} 
+0

대단히 감사합니다. 그러나 MessageBox.Show ((usernameFound? "Password": "Username") + "incorrect"); 나에게? 감사! –

+0

3 진 연산자로 간단한 조건 확인을 쓸 수 있습니다. 그것은 '점검 할 조건'과 같이 쓴다. 참이면 결과 : 거짓이면 결과. 나는 비 - 삼항 등가물로 답을 편집했다. – Kilazur

1

100 사용자 (user1..user100) 코드가 user100에 대한 그래서

for each line in file 
    check if matches 
    if yes make new form 
    else complain it isnt a match 

, 99이 아니 일치를 읽고이 있다면 : 여기

코드입니다 메시지는 전에 일치하지 않는 메일마다 하나씩 나타납니다.

당신은 논리가 잘못이

isfound=false 
for each line in file 
    check if match 
    if yes set isfound and break 

if isfound 
    show form blah 
else 
    whine not found