Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a login form which contains an user id & password to be entered by the user. Only if both fields are correct should it direct to next form. Even if one of the field is wrong, it should display a message box saying the particular filed is incorrect. A part of my code is as given below. But it is not working.

private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.txt = "USER" && textBox2.txt = "password123")
            {
                this.Hide();
                Form2 PROJ_FORM1 = new Form2();
                PROJ_FORM1.Show();
            }
            else  if (textBox1.txt != "USER" && textBox2.txt = "password123")
                MessageBox.Show("Incorrect User Id");
            else  if (textBox1.txt = "USER" && textBox2.txt != "password123")
                MessageBox.Show("Incorrect Password");
            else  if (textBox1.txt != "USER" && textBox2.txt != "password123") 
                MessageBox.Show("Incorrect User Id and Password");
        }
Posted
Comments
BillWoodruff 1-Feb-16 7:15am    
Is this Windows Forms ? If this is Windows Forms, I have some ideas for you.

1 solution

You should not get a property textBox1.txt instead textBox1.Text.however
= is assignment operator where in == is for compare equality operator.And use
C#
string.Compare()
to compare string you should not use == to compare string like this
.However below is your

C#
private void button1_Click(object sender, EventArgs e)
{
    if (string.Compare(textBox1.Text,"USER")==0 && string.Compare(textBox2.Text,"password123")==0)
    {
        this.Hide();
        Form2 PROJ_FORM1 = new Form2();
        PROJ_FORM1.Show();
    }
    else if (string.Compare(textBox1.Text, "USER") != 0 && string.Compare(textBox2.Text,"password123")==0)
        MessageBox.Show("Incorrect User Id");
    else if (string.Compare(textBox1.Text, "USER") == 0 && string.Compare(textBox2.Text,"password123")!=0)
        MessageBox.Show("Incorrect Password");
    else if (string.Compare(textBox1.Text,"USER")!=0 && string.Compare(textBox2.Text,"password123")!=0)
        MessageBox.Show("Incorrect User Id and Password");
}
 
Share this answer
 
v2
Comments
Member 12247039 1-Feb-16 3:02am    
The string.Compare() command should be used at which place?

Also i get following errors :

Error 17 'mag_2701.abc' does not contain a definition for 'label1_Click' and no extension method 'label1_Click' accepting a first argument of type 'mag_2701.abc' could be found (are you missing a using directive or an assembly reference?)

Error 18 The type or namespace name 'Form2' could not be found (are you missing a using directive or an assembly reference?)
Anisuzzaman Sumon 1-Feb-16 3:10am    
Check again
Anisuzzaman Sumon 1-Feb-16 3:12am    
If it works for you and close the thread by accepting as answer.Happy coding :)
Member 12247039 1-Feb-16 3:17am    
I am still getting those two errors that i mentioned above.
Anisuzzaman Sumon 1-Feb-16 3:20am    
Delete Form1 and add again a form named Form1 and design again.that fix it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900