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



            }
        }

OK, so the problem is the recovery window labled as Form2 is popping even when the username and pass are correct, how do I stop that?

Unless this is a homework assignment, I hope that you are not storing a username / password in plain text in a text file.

Use "String.Compare" to compare your strings.

Only open the form if the login is correct:

if (username == textBox1.Text && password == passwordtextbox.Text)
{
    MessageBox.Show("You are now successfully logged in.");
    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");
}

cgeir yea this is my class prject, so its no big deal. thanks for your concern.

Hello deceptikon, it doesn't work if i transfer it to else i already tried that method THe error I get is invalid expression term else

Edited 2 Years Ago by Parthptl

Your problem is due to encapsulation

        if (textBox1.Text == "1")
            MessageBox.Show("Hello1");
        else
            MessageBox.Show("Hello2");
        MessageBox.Show("Hello3");

If the textbox 1 text property is 1 the messagebox will show Hello1 and Hello 3

if it's not 1 the messagebox will show Hello 2 and hello 3

If you change the code as follows

            if (textBox1.Text == "1")
                MessageBox.Show("Hello1");
            else
            {
                MessageBox.Show("Hello3");
                MessageBox.Show("Hello4");
            }

The messagebox will only show Hello1 if the text property is 1

Alternatively you can use a return; statement in your code

MessageBox.Show("Username or Password seems invalid, please use email to recover password/username");
return;
This article has been dead for over six months. Start a new discussion instead.