即使密码正确,也会显示是/否窗口

时间:2014-03-24 16:58:10

标签: c#

private void button1_Click(object sender, EventArgs e)
    {

        try
        {
            var sr = new System.IO.StreamReader("C:\\" + textBox1.Text + "\\login.txt");
            username = sr.ReadLine();
            password = sr.ReadLine();
            email    = sr.ReadLine();
            sr.Close();
          if  (username == textBox1.Text && password == passwordtextbox.Text)
                MessageBox.Show("You are now successfully logged in.");


           else 
          MessageBox.Show("Username or Password seems invalid, please use email to recover password/username");
          Form2 frm = new Form2();
          // if 
          frm.Show();
          //frm.Show();
          frm.mypass = password;
          frm.myid = username;  




        }
        catch(System.IO.DirectoryNotFoundException )
        {
            MessageBox.Show("Error, please correct username/pass or recover");


        }
    }

所以,我的问题是Form2哪个是RecoveryPassWord问题是/否,用户按下是,它会提示他们输入他们的电子邮件发送密码,按否,会将其返回到主屏幕。但问题是,只有在密码错误时才会显示“是/否”形式,但密码正确时会显示。我该如何解决?

我尝试的是把这些行

Form2 frm = new Form2();
          // if 
          frm.Show();
          //frm.Show();
          frm.mypass = password;
          frm.myid = username;  
if语句中的

,因为它向我显示错误无效的表达式,所以没有用。我不知道还有什么可行的。如果你能提供帮助,请做。谢谢。

4 个答案:

答案 0 :(得分:2)

这是您的代码,正确缩进:

if (username == textBox1.Text && password == passwordtextbox.Text)
    MessageBox.Show("You are now successfully logged in.");
else 
    MessageBox.Show("Username or Password seems invalid, please use email to recover password/username");

Form2 frm = new Form2();
frm.Show();
frm.mypass = password;
frm.myid = username;

您需要花括号将逻辑组合在一起:

else
{
    MessageBox.Show("Username or Password seems invalid, please use email to recover password/username");

    Form2 frm = new Form2();
    frm.Show();
    frm.mypass = password;
    frm.myid = username;
}

我还建议将Show()替换为ShowDialog(),以防止用户在第二个表单打开时与第一个表单进行交互,并防止您的代码继续运行并尝试在用户输入密码和用户名之前获取密码和用户名。

答案 1 :(得分:0)

如果您确定正确加载了用户名和密码,可以尝试包装"否则"大括号中的子句如下:

else {
    MessageBox.Show("Username or Password seems invalid, please use email to recover password/username");
// ...
}

答案 2 :(得分:0)

如果你的if {} else {},你可能只是错过了你的大括号。如果你不使用大括号,只有else后面的直线就是if / else的一部分。所以你的Form2代码无论如何都要运行。

尝试使用:

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        var sr = new System.IO.StreamReader("C:\\" + textBox1.Text + "\\login.txt");
        username = sr.ReadLine();
        password = sr.ReadLine();
        email    = sr.ReadLine();

        sr.Close();

        if  (username == textBox1.Text && password == passwordtextbox.Text)
        {
            MessageBox.Show("You are now successfully logged in.");
        }
        else 
        {
          MessageBox.Show("Username or Password seems invalid, please use email to recover password/username");
          Form2 frm = new Form2();
          frm.Show();
          frm.mypass = password;
          frm.myid = username;  
        }
    }
    catch(System.IO.DirectoryNotFoundException)
    {
        MessageBox.Show("Error, please correct username/pass or recover");
    }
}

答案 3 :(得分:0)

用大括号括起“Else”。

if(username == textBox1.Text&& password == passwordtextbox.Text)                {                    MessageBox.Show(“您现在已成功登录。”);                 }

否则 {           MessageBox.Show(“用户名或密码似乎无效,请使用电子邮件恢复密码/用户名”);           Form2 frm = new Form2();           //如果           frm.Show();           //frm.Show();           frm.mypass =密码;           frm.myid = username;
}