为什么每次都会执行所有三个if语句? [英] Why do all three if statements get executed every time?

查看:187
本文介绍了为什么每次都会执行所有三个if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理Windows窗体。我有一个登录表单。我在登录表单中使用了三个If语句。我在登录表单中使用的代码如下所示。但是每次我输入用户名和密码时,都会执行所有三个if语句。

但是我希望每次只执行一个If语句。我该怎么纠正这个。



我尝试过:



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:

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

推荐答案

使用IF ELSE语句,它将解决您的问题,此外您还需要使用ExitSub退出事件



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

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


Quote:

为什么每次都执行所有三个if语句?

Why do all three if statements get executed every time?

这就是你在代码中要求的内容。

试试这个:

That is what you requested in your code.
Try this:

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



使用ElseIf确保只执行一次if,它不能确保你的逻辑是正确的。


the use of ElseIf ensure that only one if will be executed, it don't ensure that your logic is correct.


解决方案是使用Else或ElseIf。



The solution is to use Else or ElseIf.

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


这篇关于为什么每次都会执行所有三个if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆