If (textBox1.Text == ("admin")) + (textBox2.Text == ("admin)) 在 C# 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12677232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If (textBox1.Text == ("admin")) + (textBox2.Text == ("admin)) in C#
提问by Eksapsy
I want to use one command which will contain two "textboxes.Text" in one "If". I mean when I did this command :
我想使用一个命令,它将在一个“If”中包含两个“textboxes.Text”。我的意思是当我执行此命令时:
If (textBox1.Text == ("admin")) + (textBox2.Text == ("admin))or this If (textBox1.Text == ("admin) , textBox2.Text == admin))it isn't right.
If (textBox1.Text == ("admin")) + (textBox2.Text == ("admin))或者这 If (textBox1.Text == ("admin) , textBox2.Text == admin))是不对的。
The main code is:
主要代码是:
private void Label_Click(object sender, EventArgs e)
{
if (textBox2.Text == ("admin")) + (textBox1.Text == ("admin"))
{
Label.Text = "right";
}
else
{
Label.Text = "wrong";
errorProvider1.SetError(errorprovider, "Wrong Username or Password");
}
Namely the thing I wanted to do is if one of two textboxes is wrong the label will show that the password or the username is wrong ... any ideas ?
即我想做的事情是,如果两个文本框之一是错误的,标签将显示密码或用户名错误......有什么想法吗?
采纳答案by Jon Skeet
The syntax for an ifstatement is:
if语句的语法是:
if (condition) body
Your current code is:
您当前的代码是:
if (textBox2.Text == ("admin")) + (textBox1.Text == ("admin"))
... which is treating textBox2.Text == ("admin")as the condition, and then trying to use + (textBox1.Text == ("admin"))as the body, which isn't valid. The problems are:
... 将其textBox2.Text == ("admin")视为条件,然后尝试+ (textBox1.Text == ("admin"))用作主体,这是无效的。问题是:
- You're closing the condition too early
- You're using the wrong operator for "and"
- 你关闭条件太早了
- 您对“和”使用了错误的运算符
Additionally, you're putting parentheses around string literals for no obvious reason, reducing readability. So what you really want is:
此外,您在没有明显原因的情况下将括号放在字符串文字周围,从而降低了可读性。所以你真正想要的是:
if (textBox2.Text == "admin" && textBox1.Text == "admin")
Note that other answers have suggested using ||instead of &&- that would be an OR condition, which would show a result of "Right" if eitherof the textboxes had a value of admin. I suspect that's not what you want.
请注意,其他的答案已经使用建议||,而不是&&-这将是一个OR条件,这将显示出“正确”的结果,如果任何文本框的有值admin。我怀疑这不是你想要的。
回答by Servy
if (textBox1.Text == "admin" && textBox2.Text == "admin")
Label.Text = "right";
else
Label.Text = "wrong";
&&is the boolean AND operator. ||is the boolean OR operator.
&&是布尔 AND 运算符。 ||是布尔 OR 运算符。
回答by Balázs édes
if(textBox2.Text == "admin" && textBox1.Text == "admin")
{
//both admin
}
回答by phoog
Check the MSDN page on C# Operators.
检查C# Operators上的 MSDN 页面。
You're looking for ||(conditional or) or &&(conditional and).
您正在寻找||(条件或)或&&(条件和)。
The other name for conditional operators is "short-circuiting", because they only evaluate the second condition if they need to. In other words, with a && b, when ais false, the entire expression must be false, so the expression b is not evaluated. This is significant when bhas side effects, or when aimplies whether it is safe to evaluate b. Examples:
条件运算符的另一个名称是“短路”,因为它们仅在需要时评估第二个条件。换句话说,with a && b,whena为假,整个表达式必定为假,因此不计算表达式 b。这在b有副作用或a暗示评估是否安全时很重要b。例子:
if (MethodA() && MethodB()) //...
Here, MethodB is called only when MethodA returns true.
此处,仅当 MethodA 返回 true 时才调用 MethodB。
if (o != null && o.Equals(p)) //...
This is safe (and common), because it saves us from the NullReferenceExceptionwhen o is null.
这是安全的(也是常见的),因为它使我们免于NullReferenceExceptiono 为空的情况。
You can also use the non-short-circuiting versions of these operators (|and &) with boolean expressions, but this is so rare that most programmers will read it, at first glance, as a mistake; it's better to be more explicit if you want the code always to evaluate both expressions.
您还可以将这些运算符 ( |and &)的非短路版本与布尔表达式一起使用,但这种情况很少见,以至于大多数程序员乍一看会认为这是错误的;如果您希望代码始终评估两个表达式,最好更明确。
回答by MikeB
Seems like you need a simple or? Use the double vertical pipe ||
好像你需要一个简单的或?使用双立管 ||
private void Label_Click(object sender, EventArgs e)
{
if (textBox2.Text == ("admin") || textBox1.Text == ("admin"))
{
Label.Text = "right";
}
else
{
Label.Text = "wrong";
errorProvider1.SetError(errorprovider, "Wrong Username or Password");
}
}
回答by Yván Ecarri
private void Label_Click(object sender, EventArgs e)
{
if ((textBox2.Text == "admin") || (textBox1.Text == "admin"))
{
Label.Text = "right";
}
else
{
Label.Text = "wrong";
errorProvider1.SetError(errorprovider, "Wrong Username or Password");
}
}
回答by Max
You need to use the "and" operator which is "&&" in C#.
您需要使用“and”运算符,即 C# 中的“&&”。
if (textBox1.Text == ("admin")) && (textBox2.Text == ("admin))
回答by Alex
Your expression is incorrect. The correct syntax is
你的表达不正确。正确的语法是
if (A && B) { ... }
So in your case it should be
所以在你的情况下应该是
if(textBox1.Text.Equals("admin") && textBox2.Text.Equals("admin")) { ... }
You may want to read up a bit on Boolean algebra if this logic is confusing you.
如果这个逻辑让你感到困惑,你可能想阅读一些布尔代数。
Note the other changes we've all suggested as well - you had extra brackets and should be using Equals()for string comparison as well.
请注意我们也都建议的其他更改 - 您有额外的括号,也应该Equals()用于字符串比较。

