Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on windows forms. I have a log in form. I am using three If statements in the log in form. The code i have used in the log in form is as given below. However every time i enter the user name and password, all the three if statements get executed.
But i want only one If statement to get executed everytime. How do i rectify this.

What I have tried:

VB
Public Class WELCOME
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If TextBox1.Text = "EMPLOYEE" And TextBox2.Text = "PASSWORD" Then
            Me.Hide()
            PROBLEMS.Show()
        End If

        If TextBox1.Text = "EMPLOYEE" And TextBox2.Text IsNot "PASSWORD" Then
            MessageBox.Show("Incorrect Password")
        End If

        If TextBox1.Text IsNot "EMPLOYEE" And TextBox2.Text = "PASSWORD" Then
            MessageBox.Show("Incorrect User Id")
        End If

    End Sub
End Class
Posted
Updated 21-Mar-16 21:13pm
v2
Comments
F. Xaver 22-Mar-16 3:41am    
IsNot is comparing object references, not the Object it self.
I'm pretty sure you better go with '<>' operator

Use IF ELSE statement, it will resolve your issue, additionally you need to use ExitSub to exit from event

VB
If TextBox1.Text.toUpper() = "EMPLOYEE" And TextBox2.Text.toUpper() = "PASSWORD" Then
 Me.Hide()
 PROBLEMS.Show()

Else If (TextBox1.Text.toUpper() <> "EMPLOYEE") Then
MessageBox.Show("Incorrect Username")
Exit sub

ELSE If (TextBox2.Text.toUpper() <> "PASSWORD") Then
MessageBox.Show("Incorrect Password")
Exit sub
End If

hope it helps
 
Share this answer
 
v2
Comments
Member 12247039 22-Mar-16 3:27am    
Thank You. It does solve my purpose without using the Exit Sub.
Sascha Lefèvre 22-Mar-16 5:37am    
if/if-else is not a loop! If you're looking for a word how to call it: statement
koolprasad2003 22-Mar-16 6:09am    
Ooops ...Typo
Thanks for correction.
Quote:
Why do all three if statements get executed every time?
That is what you requested in your code.
Try this:
VB
Public Class WELCOME
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If TextBox1.Text = "EMPLOYEE" And TextBox2.Text = "PASSWORD" Then
	Me.Hide()
	PROBLEMS.Show()
	
ElseIf TextBox1.Text = "EMPLOYEE" And TextBox2.Text IsNot "PASSWORD" Then
	MessageBox.Show("Incorrect Password")

ElseIf TextBox1.Text IsNot "EMPLOYEE" And TextBox2.Text = "PASSWORD" Then
	MessageBox.Show("Incorrect User Id")
End If

End Sub
End Class

the use of ElseIf ensure that only one if will be executed, it don't ensure that your logic is correct.
 
Share this answer
 
v3
Comments
Member 12247039 22-Mar-16 3:25am    
Thank You
Sascha Lefèvre 22-Mar-16 5:47am    
While ElseIf's are a good thing to suggest of course, it's not the actual problem. The actual problem is that the inquirer apparently thinks that IsNot would be the counterpart to =
Instead it's checking for referential equality.
Michael_Davies 22-Mar-16 12:16pm    
Well spotted, that will be his next problem, regardless each statement would execute as nothing stops the execution flow after testing any of the IF's.
The solution is to use Else or ElseIf.

VB
If TextBox1.Text = "EMPLOYEE" And TextBox2.Text = "PASSWORD" Then
Me.Hide()
PROBLEMS.Show()
ElseIf TextBox1.Text = "EMPLOYEE" And TextBox2.Text IsNot "PASSWORD" Then
MessageBox.Show("Incorrect Password")
ElseIf TextBox1.Text IsNot "EMPLOYEE" And TextBox2.Text = "PASSWORD" Then
MessageBox.Show("Incorrect User Id")
End If
 
Share this answer
 
Comments
Member 12247039 22-Mar-16 3:25am    
Thank You Michael
 
Share this answer
 

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