C# 如果没有与文本文件C匹配的内容,请使用MessageBox提供反馈#

C# 如果没有与文本文件C匹配的内容,请使用MessageBox提供反馈#,c#,logic,C#,Logic,我目前正在建立一个登录系统,我想在用户名或密码不正确时提供反馈 下面是文本文件的内容,我正在从中阅读详细信息: Ryan:password Username:password 当我输入Ryan和password时,它工作正常,并将我带到下一个表单。 但是,当我输入用户名和密码时,它首先会出现“用户名不正确”消息框,然后在我关闭该消息框后,它会将我带到下一个表单 我希望它能直接将我带到下一个表单,而不首先显示用户名不正确的MessageBox,即使我在第二行输入了详细信息。将来文本文件中将有更多

我目前正在建立一个登录系统,我想在用户名或密码不正确时提供反馈

下面是文本文件的内容,我正在从中阅读详细信息:

Ryan:password
Username:password
当我输入
Ryan
password
时,它工作正常,并将我带到下一个表单。 但是,当我输入
用户名
密码
时,它首先会出现“用户名不正确”消息框,然后在我关闭该消息框后,它会将我带到下一个表单

我希望它能直接将我带到下一个表单,而不首先显示用户名不正确的MessageBox,即使我在第二行输入了详细信息。将来文本文件中将有更多行

任何帮助都将不胜感激,谢谢

代码如下:

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");
            }
        }
    }
}

在senario中,如果您有100个用户(user1..user100) 你的代码是

for each line in file
  check if matches 
     if yes make new form 
     else complain it isnt a match
因此,对于user100,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

逻辑是错误的

问问你自己,你什么时候应该停止浏览你的证书列表

假设用户名是唯一的,我认为只有一种情况会破坏循环,那就是“用户名已找到”

一旦您在列表中找到输入用户名,您就知道循环必须中断。然后,您只需检查密码是否正确

如果密码正确,您可以打开新窗口并
返回
您的功能,它已经完成了它的工作

循环结束后,您将放入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");
}

很好,谢谢。但是,您能解释一下行:MessageBox.Show((usernameFound?“Password”:“Username”)+“error”);对我来说?谢谢这是一个三元运算符,它允许您编写简短的条件检查。它写的像是要检查的
条件?如果结果为真:如果结果为假
。我用非三元等价物编辑了答案。